Bird
Raised Fist0
MongoDBquery~20 mins

Atlas search overview in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Atlas Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Find documents matching a text phrase using Atlas Search
Given a collection named books with an Atlas Search index on the title field, what is the output of this query?
MongoDB
db.books.aggregate([
  {
    $search: {
      text: {
        query: "adventure",
        path: "title"
      }
    }
  },
  { $project: { title: 1, _id: 0 } }
])
A[{ "title": "The Great Adventure" }, { "title": "Adventure in the Mountains" }]
B[{ "title": "The Great Adventure" }, { "title": "Cooking Recipes" }]
C[{ "title": "Cooking Recipes" }, { "title": "Gardening Tips" }]
D[]
Attempts:
2 left
💡 Hint
Think about which titles contain the word 'adventure'.
🧠 Conceptual
intermediate
1:30remaining
Understanding Atlas Search index types
Which of the following best describes the purpose of an Atlas Search index in MongoDB?
AIt automatically encrypts data at rest in the database.
BIt stores backup copies of the database for disaster recovery.
CIt enables fast, full-text search queries on specified fields using Lucene technology.
DIt manages user authentication and access control.
Attempts:
2 left
💡 Hint
Atlas Search uses a popular open-source search engine technology.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Atlas Search query
What error does this query raise?
MongoDB
db.products.aggregate([
  {
    $search: {
      text: {
        query: "smartphone",
        path: ["name", "description"]
      }
    }
  }
])
ASyntaxError: The 'path' field must be a string, not an array.
BRuntimeError: The $search stage is not supported in aggregate pipelines.
CTypeError: The 'query' field must be an array of strings.
DNo error; the query runs successfully and returns matching documents.
Attempts:
2 left
💡 Hint
Check the Atlas Search documentation for the 'text' operator's 'path' field type.
❓ optimization
advanced
2:30remaining
Improve performance of an Atlas Search query filtering by category
You want to find documents with 'laptop' in the 'title' and category 'electronics'. Which query is more efficient?
AUse $search with compound operator combining text on 'title' and equals on 'category'.
BUse $match on 'category' first, then $search with text operator on 'title'.
CUse $search with text operator on 'title' and then $match on 'category' after $search.
DUse $match on both 'title' and 'category' without $search.
Attempts:
2 left
💡 Hint
Atlas Search compound operator can combine multiple conditions efficiently.
🔧 Debug
expert
3:00remaining
Why does this Atlas Search query return no results?
Given this query, why might it return an empty result set?
MongoDB
db.orders.aggregate([
  {
    $search: {
      text: {
        query: "urgent",
        path: "status"
      }
    }
  }
])
AThe query syntax is invalid because 'query' must be an array, not a string.
BThe 'status' field is not included in the Atlas Search index, so no matches are found.
CThe $search stage cannot be used on fields with string values.
DThe collection 'orders' does not exist, causing the query to return no results.
Attempts:
2 left
💡 Hint
Check if the field you search is indexed by Atlas Search.

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

  1. Step 1: Understand the role of $search

    The $search stage is used in aggregation pipelines to perform text search queries on MongoDB collections.
  2. Step 2: Differentiate from other operations

    Updating, deleting, or creating indexes are not functions of $search. It is specifically for searching text.
  3. Final Answer:

    To perform text search queries on collections -> Option B
  4. 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?
easy
A. { $search: { find: { query: "apple", field: "name" } } }
B. { $search: { text: { search: "apple", field: "name" } } }
C. { $search: { query: "apple", path: "name" } }
D. { $search: { text: { query: "apple", path: "name" } } }

Solution

  1. Step 1: Recall the correct $search syntax

    The $search stage uses a text operator with query and path fields to specify the search term and field.
  2. 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.
  3. Final Answer:

    { $search: { text: { query: "apple", path: "name" } } } -> Option D
  4. Quick Check:

    Correct keys: text, query, path [OK]
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']?
[{ $search: { text: { query: "apple", path: "name" } } }]
medium
A. Only the document with name 'apple'
B. Documents with names 'apple' and 'pineapple'
C. Documents with names 'banana' and 'pineapple'
D. All documents in the collection

Solution

  1. Step 1: Understand text search behavior

    The $search with text operator performs full-text search matching whole terms after tokenization. It does not perform substring matching by default.
  2. 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.
  3. Final Answer:

    Only the document with name 'apple' -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify missing field

    The pipeline is missing the 'path' field, so MongoDB does not know which field to search.
  3. Final Answer:

    Missing the 'path' field specifying which field to search -> Option A
  4. 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?
hard
A. { $search: { text: { query: "mongodb", path: "title" }, text: { query: "mongodb", path: "description" } } }
B. { $search: { compound: { must: [{ text: { query: "mongodb", path: "title" } }, { text: { query: "mongodb", path: "description" } }] } } }
C. { $search: { text: { query: "mongodb", path: ["title", "description"] } } }
D. { $search: { text: { query: "mongodb", path: "title" } }, filter: { text: { query: "mongodb", path: "description" } } }

Solution

  1. Step 1: Understand searching multiple fields

    The text operator allows specifying multiple fields by passing an array to the path parameter.
  2. Step 2: Evaluate options for multiple field search

    { $search: { text: { query: "mongodb", path: ["title", "description"] } } } correctly uses an array for path to search both 'title' and 'description' (OR semantics). { $search: { text: { query: "mongodb", path: "title" }, text: { query: "mongodb", path: "description" } } } has invalid syntax with duplicate text keys. { $search: { compound: { must: [{ text: { query: "mongodb", path: "title" } }, { text: { query: "mongodb", path: "description" } }] } } } uses compound with must requiring both fields to match (AND). { $search: { text: { query: "mongodb", path: "title" } }, filter: { text: { query: "mongodb", path: "description" } } } has invalid syntax.
  3. Final Answer:

    { $search: { text: { query: "mongodb", path: ["title", "description"] } } } -> Option C
  4. Quick Check:

    Use array in path for multiple fields [OK]
Hint: Use array in 'path' to search multiple fields [OK]
Common Mistakes:
  • Using compound must instead of array for either/or
  • Repeating text operator keys incorrectly
  • Misusing filter stage inside $search
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard