Construct search queries
You can construct Atlas Search queries with the $search
aggregation pipeline stage.
In this section, we'll build an aggregation pipeline with the $search
stage which performs full-text search using an Atlas Search index.
In this section, you'll use the text
operator which performs a textual analysis search.
Aggregations in the Atlas UI
Navigate to the Collections tab of your database deployment.
Navigate to the Aggregation tab from the navbar under your collection details.
Click the Add Stage button and type $search in the select input.
Add the following code for the
$search
stage.{
index: "fulltextsearch",
text: {
query: "harry potter",
path: "title"
}
}The stage uses the "fulltextsearch" index. You don't need to explicitly define the index if it's "default" but you can keep it for clarity.
The
text
operator will search for "harry potter" in thetitle
field. You should see a collection of documents returned on the right.Click the Add Stage button, scroll down, and select $project for Stage 2.
Add the following implementation for the
$project
stage to filter the returned fields.{
title: 1,
year: 1,
"imdb.rating": 1,
fullplot: 1,
poster: 1,
released: 1,
genres: 1
}infoThis is not an exact match search but a textual analyzed search. The full text in the
title
field is “Harry Potter and the Half-Blood Prince”. If instead of an aggregation pipeline and$search
, you usecollection.find({ title: "harry potter" })
, you won't get this document in the results.