📖 Scenario: You are building a simple product catalog search feature for an online store using MongoDB Atlas Search. This will help users find products by name and description quickly.
🎯 Goal: Create a MongoDB collection with product data, configure an Atlas Search index, write a search query using the $search stage, and retrieve matching products.
📋 What You'll Learn
Create a MongoDB collection named products with sample product documents
Add a configuration variable for the search index name
Write an aggregation query using the $search stage with the index name
Complete the query to return product names and descriptions matching a search term
💡 Why This Matters
🌍 Real World
Atlas Search helps build fast and relevant search features for applications like online stores, catalogs, and content libraries.
💼 Career
Understanding Atlas Search is valuable for roles involving MongoDB database management, backend development, and building search-driven user experiences.
Progress0 / 4 steps
1
DATA SETUP: Create the products collection with sample data
Create a MongoDB collection called products and insert these exact documents: { _id: 1, name: "Red T-Shirt", description: "A bright red t-shirt made of cotton" }, { _id: 2, name: "Blue Jeans", description: "Comfortable blue denim jeans" }, and { _id: 3, name: "Green Hat", description: "A stylish green hat for sunny days" }.
MongoDB
Hint
Use db.products.insertMany([...]) with the exact documents listed.
2
CONFIGURATION: Define the search index name
Create a variable called searchIndex and set it to the string "default", which is the name of the Atlas Search index you will use.
MongoDB
Hint
Use const searchIndex = "default" to define the index name.
3
CORE LOGIC: Write the Atlas Search aggregation query
Write an aggregation query on the products collection using db.products.aggregate(). Use the $search stage with index: searchIndex and a text operator to search the name and description fields for the term "red". Store the query in a variable called searchQuery.
MongoDB
Hint
Use db.products.aggregate([{ $search: { index: searchIndex, text: { query: "red", path: ["name", "description"] } } }]) and assign it to searchQuery.
4
COMPLETION: Add a projection stage to return only name and description
Add a $project stage to the searchQuery aggregation pipeline to return only the name and description fields. Update the searchQuery variable to include this stage.
MongoDB
Hint
Add { $project: { _id: 0, name: 1, description: 1 } } as the second stage in the aggregation pipeline.
Practice
(1/5)
1. What is the primary purpose of the $search stage in MongoDB Atlas Search?
easy
A. To update documents in a collection
B. To perform text search queries on collections
C. To delete documents based on a condition
D. To create indexes on collections
Solution
Step 1: Understand the role of $search
The $search stage 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 B
Quick Check:
$search = text search [OK]
Hint: Remember: $search is for finding text, not modifying data [OK]
Common Mistakes:
Confusing $search with update or delete operations
Thinking $search creates indexes
Assuming $search works outside aggregation
2. Which of the following is the correct syntax to use the $search stage in an aggregation pipeline?
The $search stage uses a text operator with query and path fields 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 like find, missing text, or wrong field names.
Hint: Look for keys: text, query, and path in $search [OK]
Common Mistakes:
Using 'find' instead of 'text' inside $search
Omitting 'text' operator
Using 'field' instead of 'path'
3. Given the following aggregation pipeline, what documents will be returned if the collection has documents with names: ['apple', 'banana', 'pineapple']?
The $search with text operator 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 A
Quick Check:
Whole term matches only [OK]
Hint: Text search matches whole terms, not substrings [OK]
Common Mistakes:
Assuming substring matches like 'pineapple' for 'apple'
Including documents without the query term
Thinking $search returns all documents
4. Identify the error in this aggregation pipeline using Atlas Search:
[{ $search: { text: { query: "apple" } } }]
medium
A. Missing the 'path' field specifying which field to search
B. The 'query' field should be named 'search'
C. The $search stage cannot be used inside aggregation
D. The 'text' operator is invalid inside $search
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 A
Quick Check:
Always include 'path' with 'query' in $search text [OK]
Hint: Always specify 'path' when using $search text operator [OK]
Common Mistakes:
Omitting 'path' field
Renaming 'query' to 'search'
Thinking $search can't be in aggregation
5. You want to search for documents where either the 'title' or 'description' fields contain the word 'mongodb'. Which $search operator should you use in your aggregation pipeline?