0
0
MongoDBquery~30 mins

Atlas search overview in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Atlas Search Overview
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Add { $project: { _id: 0, name: 1, description: 1 } } as the second stage in the aggregation pipeline.