Bird
Raised Fist0
MongoDBquery~20 mins

Text indexes for search in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Text Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Find documents matching a text search
Given a MongoDB collection articles with a text index on the content field, what will be the output of this query?

db.articles.find({ $text: { $search: "coffee" } })

Assume the collection has these documents:
[{ _id: 1, content: "I love coffee and tea." }, { _id: 2, content: "Tea is great." }, { _id: 3, content: "Coffee is energizing." }]
MongoDB
db.articles.find({ $text: { $search: "coffee" } })
A[{ _id: 2, content: "Tea is great." }]
B[{ _id: 1, content: "I love coffee and tea." }, { _id: 3, content: "Coffee is energizing." }]
C[{ _id: 1, content: "I love coffee and tea." }, { _id: 2, content: "Tea is great." }, { _id: 3, content: "Coffee is energizing." }]
D[]
Attempts:
2 left
💡 Hint
Text search matches documents containing the search word in the indexed fields.
🧠 Conceptual
intermediate
1:30remaining
Understanding text index weights
In MongoDB, when creating a text index on multiple fields with different weights, what effect do the weights have on text search results?
AWeights specify the maximum length of words indexed in each field.
BWeights determine how many times a word must appear in a field to be indexed.
CWeights limit the number of documents returned by the text search query.
DFields with higher weights contribute more to the relevance score, making documents with matches in those fields rank higher.
Attempts:
2 left
💡 Hint
Think about how search results are ranked by relevance.
📝 Syntax
advanced
1:30remaining
Correct syntax for creating a text index on multiple fields
Which of the following commands correctly creates a text index on the fields title and description in a MongoDB collection products?
Adb.products.createIndex({ title: text, description: text })
Bdb.products.createIndex({ title: 'text', description: 'text' })
Cdb.products.createIndex({ title: "text", description: "text" })
Ddb.products.createIndex({ title: text(), description: text() })
Attempts:
2 left
💡 Hint
Text index type must be a string.
❓ optimization
advanced
2:00remaining
Improving text search performance with index filters
You have a large MongoDB collection with a text index on the comments field. You want to speed up text searches by limiting the index to documents where status is "active". Which approach achieves this?
ACreate a partial text index with a filter: db.collection.createIndex({ comments: "text" }, { partialFilterExpression: { status: "active" } })
BAdd a regular index on the status field only.
CUse a text index on comments and filter status in the query without index changes.
DCreate a compound index on { status: 1, comments: "text" }.
Attempts:
2 left
💡 Hint
Partial indexes index only documents matching a filter.
🔧 Debug
expert
2:00remaining
Why does this text search query return no results?
You created a text index on the summary field of a MongoDB collection. You run this query:

db.books.find({ $text: { $search: "" } })

But it returns no documents. Why?
AThe $search string is empty, so no terms to match; the query returns no results.
BText indexes require the $search string to be at least 3 characters long.
CThe text index was not created properly and is missing.
DMongoDB does not support text search on the summary field.
Attempts:
2 left
💡 Hint
Think about what an empty search string means for matching.

Practice

(1/5)
1. What is the main purpose of creating a text index in MongoDB?
easy
A. To encrypt text data for security
B. To enable fast searching of text within string fields
C. To store large text files inside the database
D. To sort documents by text fields alphabetically

Solution

  1. Step 1: Understand what a text index does

    A text index allows MongoDB to quickly search text inside string fields of documents.
  2. Step 2: Compare options with the purpose

    Sorting, storing files, or encrypting are not functions of text indexes.
  3. Final Answer:

    To enable fast searching of text within string fields -> Option B
  4. Quick Check:

    Text index = fast text search [OK]
Hint: Text index = fast search inside text fields [OK]
Common Mistakes:
  • Confusing text index with sorting index
  • Thinking text index stores files
  • Assuming text index encrypts data
2. Which of the following is the correct syntax to create a text index on the description field in a MongoDB collection named products?
easy
A. db.products.createTextIndex({ description: 1 })
B. db.products.createIndex({ description: 'string' })
C. db.products.createIndex({ description: 'text' })
D. db.products.createIndex({ description: 1 })

Solution

  1. Step 1: Recall the syntax for creating a text index

    The correct method is createIndex with the field set to 'text' type.
  2. Step 2: Check each option

    db.products.createIndex({ description: 'text' }) uses createIndex with { description: 'text' }, which is correct. Others use wrong methods or types.
  3. Final Answer:

    db.products.createIndex({ description: 'text' }) -> Option C
  4. Quick Check:

    Use createIndex with 'text' type [OK]
Hint: Use createIndex({field: 'text'}) to make text index [OK]
Common Mistakes:
  • Using 'string' instead of 'text' for index type
  • Using createTextIndex (not a valid method)
  • Creating normal ascending index (1) instead of text
3. Given a collection articles with a text index on fields title and content, what will the following query return?
db.articles.find({ $text: { $search: "mongodb tutorial" } })
medium
A. Documents containing both words 'mongodb' and 'tutorial' in title or content
B. Documents containing exactly the phrase 'mongodb tutorial' only
C. Documents containing either 'mongodb' or 'tutorial' in any field
D. Documents sorted alphabetically by title

Solution

  1. Step 1: Understand $text and $search behavior

    The $search operator looks for documents containing all search words (AND behavior) in indexed text fields.
  2. Step 2: Analyze the query

    The query searches for documents with both 'mongodb' and 'tutorial' in either title or content fields.
  3. Final Answer:

    Documents containing both words 'mongodb' and 'tutorial' in title or content -> Option A
  4. Quick Check:

    $search finds all words, not exact phrase [OK]
Hint: $search finds all words, not exact phrase [OK]
Common Mistakes:
  • Thinking $search matches exact phrase only
  • Assuming $search matches any single word (OR)
  • Expecting sorting by title automatically
4. You tried to create two text indexes on a collection: one on title and another on description. What is the problem with this approach?
medium
A. MongoDB allows only one text index per collection
B. Text indexes must be created on numeric fields only
C. You must create text indexes using createTextIndex method
D. Text indexes cannot be created on multiple fields

Solution

  1. Step 1: Recall MongoDB text index limitation

    MongoDB allows only one text index per collection, but it can cover multiple fields.
  2. Step 2: Evaluate the options

    MongoDB allows only one text index per collection correctly states the limitation. Others are incorrect about field types or methods.
  3. Final Answer:

    MongoDB allows only one text index per collection -> Option A
  4. Quick Check:

    One text index per collection rule [OK]
Hint: Only one text index allowed per collection [OK]
Common Mistakes:
  • Trying to create multiple text indexes on same collection
  • Using wrong method name for creating text index
  • Thinking text indexes only work on numbers
5. You want to create a text index on title and summary fields but give title twice the search weight of summary. Which is the correct way to create this weighted text index?
hard
A. db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { summary: 2, title: 1 } })
B. db.books.createIndex({ title: 'text', summary: 'text' }, { weight: { title: 2, summary: 1 } })
C. db.books.createIndex({ title: 'text', summary: 'text' }, { weight: { summary: 2, title: 1 } })
D. db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { title: 2, summary: 1 } })

Solution

  1. Step 1: Recall syntax for weighted text indexes

    Weights option is an object named weights specifying field weights.
  2. Step 2: Check options for correct spelling and values

    db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { title: 2, summary: 1 } }) uses weights correctly and assigns title weight 2, summary 1 as required.
  3. Final Answer:

    db.books.createIndex({ title: 'text', summary: 'text' }, { weights: { title: 2, summary: 1 } }) -> Option D
  4. Quick Check:

    Use 'weights' option with correct field weights [OK]
Hint: Use 'weights' option with field weights object [OK]
Common Mistakes:
  • Using 'weight' instead of 'weights' option
  • Swapping weights between fields
  • Not specifying weights object correctly
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