Video Transcript (English)
Going back to our movie catalog, what if we want to find movies that were released during a certain month? How do we do that? We know we can’t use the equals search operator because it can only search for a specific date.
In this video, you’ll learn about the near and range search operators.
These allow us to search documents corresponding to specific numbers, dates, or locations.
The near search operator performs a search for dates, numbers, or geographic locations nearest to a given value or point.
This operator returns all documents in the collection ordered by their proximity to the provided value
A common use case for the near operator is for plotting locations near a given point on a map.
Let’s take a look at an example of using the near operator to search for movies released around a particular date.
For this, we’ll use the familiar plotReleasedIndex search index, which indexes the movie plot and released fields in our movies collection. Next, let’s create our search query.
First, we specify the plotReleasedIndex as our search index.
Next, we specify the near operator, and include the released path because that’s the field we want to query.
After that, we provide an origin. This is the starting point of our query: May 17th, 1999. In other words, we are looking for documents near this value.
Finally, we provide a pivot value to help score the results.
The pivot value can best be described as the maximum distance from the origin that leads to a score of 0.5
But what do we mean by distance? The distance is the difference between the indexed field’s value and the specified origin.
A result with a distance of zero from the origin is given the highest score of 1.
As we move away from the origin, the score drops below 1 until it reaches 0.5 at the pivot value.
Results past the pivot value are scored progressively below 0.5.
A pivot value can be an integer or a floating point, depending on the data type used by the origin, and its value cannot be zero.
When the origin is a date or a number, we need to use an integer.
Because we’re searching for a nearest date in our example, the distance from the origin will be in milliseconds and so is our pivot value.
When the origin is a GeoJSON location, the pivot value is measured in meters and must be specified as an integer or floating point number.
Back in our example, we’re looking for movies released within thirty days of May 17, 1999.
This comes out to around 2.6 billion milliseconds.
Before we run this query, let’s add a projection stage so our results will have only the title, release date, and the score fields of each document.
Let’s see what we get after we run this query.
Here, we can see each movie's release date.
We’ve also included a score field.
The score is calculated by dividing the pivot value by the pivot plus the distance from the origin.
Documents with a score closest to 1 are the documents closest to the origin release date of May 17th.
Documents with a score of 0.5 are at the maximum distance set by the pivot. So these are about thirty days from May 17th. In this example, the date is April 17th.
Documents with a score less than 0.5 are beyond the distance of the pivot. This one is more than thirty days away from our target date, March 31st.
Similar to the near search operator, we have the range operator.
The range operator performs a search based on a range of numbers or dates.
Its syntax is similar to a MongoDB query using comparison operators such as Greater than, Greater than or equal to, Less than, or less than or equal to.
However, unlike a MongoDB query, we do not need to prefix the comparison operator with a dollar-sign.
Let’s use the range operator to return all documents with movies released between January 1st, 1994 and January 1st, 1999.
Like before, we specify the search index we're going to use.
Next, we specify the range search operator.
Then we set the path to the released field.
After that, we use the comparison operators to search for documents that fall in a range of dates. In this case, we’re using greater than and less than.
Finally, we’ll add a projection stage to return only the title and released fields of each document.
From our results, we can see every movie with a release date between January 1st, 1994 and January 1st, 1999.
Awesome job. In this video, you learned that the near search operator performs a search for dates, numbers, or geographic locations nearest to a given value or point.
It also returns all documents in the collection ordered by their proximity to the given point.
Then, you learned that the range operator performs a search based on a range of numbers or dates, and returns only the documents within that range.
In this video, we focused on just two search operators, but there are many more available to you. For a full list of search operators, check out the MongoDB documentation.