Atlas Search helps you find information quickly inside your MongoDB data. It makes searching easier and faster.
Atlas search overview in MongoDB
db.collection.aggregate([
{
$search: {
index: "default",
text: {
query: "search term",
path: "fieldName"
}
}
}
])The $search stage is used inside an aggregation pipeline.
You specify the search index and the text query with the field to search.
db.books.aggregate([
{
$search: {
text: {
query: "history",
path: "title"
}
}
}
])db.products.aggregate([
{
$search: {
text: {
query: "laptop",
path: ["name", "description"]
}
}
}
])This query searches the 'movies' collection for documents where the 'genre' field contains 'adventure'. It returns the first 3 matching movies.
db.movies.aggregate([
{
$search: {
text: {
query: "adventure",
path: "genre"
}
}
},
{
$limit: 3
}
])You must create a search index in Atlas before using $search.
$search works only inside aggregation pipelines.
Atlas Search supports many types of queries like text, autocomplete, phrase, and more.
Atlas Search lets you quickly find text inside MongoDB collections.
Use the $search stage in aggregation to run search queries.
It supports searching multiple fields and different query types.
Practice
$search stage in MongoDB Atlas Search?Solution
Step 1: Understand the role of
The$search$searchstage is used in aggregation pipelines to perform text search queries on MongoDB collections.Step 2: Differentiate from other operations
Updating, deleting, or creating indexes are not functions of$search. It is specifically for searching text.Final Answer:
To perform text search queries on collections -> Option BQuick Check:
$search= text search [OK]
- Confusing $search with update or delete operations
- Thinking $search creates indexes
- Assuming $search works outside aggregation
$search stage in an aggregation pipeline?Solution
Step 1: Recall the correct
The$searchsyntax$searchstage uses atextoperator withqueryandpathfields to specify the search term and field.Step 2: Compare options to syntax
{ $search: { text: { query: "apple", path: "name" } } } matches the correct syntax. Other options use incorrect keys likefind, missingtext, or wrong field names.Final Answer:
{ $search: { text: { query: "apple", path: "name" } } } -> Option DQuick Check:
Correct keys: text, query, path [OK]
- Using 'find' instead of 'text' inside $search
- Omitting 'text' operator
- Using 'field' instead of 'path'
[{ $search: { text: { query: "apple", path: "name" } } }]Solution
Step 1: Understand text search behavior
The$searchwithtextoperator performs full-text search matching whole terms after tokenization. It does not perform substring matching by default.Step 2: Apply to given names
'apple' matches the query term exactly. 'pineapple' is tokenized as a single term "pineapple", which does not match "apple". 'banana' does not match.Final Answer:
Only the document with name 'apple' -> Option AQuick Check:
Whole term matches only [OK]
- Assuming substring matches like 'pineapple' for 'apple'
- Including documents without the query term
- Thinking $search returns all documents
[{ $search: { text: { query: "apple" } } }]Solution
Step 1: Check required fields in $search text operator
The 'text' operator requires both 'query' and 'path' fields to specify the search term and the field to search.Step 2: Identify missing field
The pipeline is missing the 'path' field, so MongoDB does not know which field to search.Final Answer:
Missing the 'path' field specifying which field to search -> Option AQuick Check:
Always include 'path' with 'query' in $search text [OK]
- Omitting 'path' field
- Renaming 'query' to 'search'
- Thinking $search can't be in aggregation
$search operator should you use in your aggregation pipeline?Solution
Step 1: Understand searching multiple fields
Thetextoperator allows specifying multiple fields by passing an array to thepathparameter.Step 2: Evaluate options for multiple field search
{ $search: { text: { query: "mongodb", path: ["title", "description"] } } } correctly uses an array forpathto search both 'title' and 'description' (OR semantics). { $search: { text: { query: "mongodb", path: "title" }, text: { query: "mongodb", path: "description" } } } has invalid syntax with duplicatetextkeys. { $search: { compound: { must: [{ text: { query: "mongodb", path: "title" } }, { text: { query: "mongodb", path: "description" } }] } } } usescompoundwithmustrequiring both fields to match (AND). { $search: { text: { query: "mongodb", path: "title" } }, filter: { text: { query: "mongodb", path: "description" } } } has invalid syntax.Final Answer:
{ $search: { text: { query: "mongodb", path: ["title", "description"] } } } -> Option CQuick Check:
Use array in path for multiple fields [OK]
- Using compound must instead of array for either/or
- Repeating text operator keys incorrectly
- Misusing filter stage inside $search
Ready to prove it?
Pass the test and this pattern moves unknown
