0
0
MongoDBquery~20 mins

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

Choose your learning style9 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.