Lesson 4: $search operators: text and equals / Learn

Press O for more options
3:17

Video Transcript (English)

Now that we have data types under our belt let’s look at the set of operators supported by Atlas Search.

In this video, you’ll learn two common search operators: text and equals.

Before we begin, let’s recap how to create a search query.

First, we use the aggregation framework with the search stage, which must be the first stage in the aggregation pipeline.

Inside the search stage, we specify the search index and the query operator.

We’ll use the plotReleasedIndex search index, which indexes the movie plot and released fields in our movies collection.

With this in mind, let’s take a look at some examples of search queries that use the text and equals operators.

First up is the text operator. It performs a full-text search of data in our search index.

We’ll use our plotReleasedIndex with the text operator to search for movies with a plot that contains the word “nature”.

First, we define the text operator.

Then, we provide a query field.

This is where we define the term or phrase that we’ll search for. In this case, we will query for movie plots that include the term “nature”.

Next, we provide the path, which is the field we want to query. Remember that this field must be covered by the search index via dynamic mapping or static mapping as a `string` field mapping type. In this case, we are using the plot field.

Optionally, we can add an option to enable fuzzy search. Fuzzy search helps users find what they are searching for even if there are typos.

For example, if we misspelled nature by mixing up the “r” and “e”, fuzzy search would accommodate for this and return similar results as if nature was spelled correctly.

We’ll talk more about fuzzy search later.

Finally, we’ll add a projection stage to return only the title and plot fields of each document.

When we run this query, we get these results.

Each of the plot fields contain the term nature.

Now we’ll move on to the equals operator. It returns documents with a field that matches a specified value.

The equals operator can be used only with certain data types, such as: Booleans, Dates, objectIds, Numbers , Text indexed as tokens, and arrays.

The array type must contain one of the previously mentioned data types.

If one of the elements in the array matches the query, the document will be added to the results.

Let’s test the equals operator by searching for movies that were released on March 31st, 1999.

We’ll use the same plotReleasedIndexwe created earlier on the plot and released fields.

Next, we specify the equals operator.

After that, we define the field path that we want to query, which in this case is the released field.

Then, we use the value field to determine the date we are searching for: March 31st, 1999.

Like earlier, we’ll add a projection stage to return only the title and released fields of each document.

When we run this query, we get a list of movies that were released on March 31st, 1999.

Great job! To recap, the search stage is used to perform search queries using the aggregation framework.

The search stage accepts many search operators. In this video we saw the text and equals operators in action.

The text operator performs a full text search.

The equals operator returns documents with a field that matches the specified value.

For a full list of search operators, check out the MongoDB documentation.