0
0
MongoDBquery~20 mins

Text search with text indexes 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?
MongoDB
db.articles.find({ $text: { $search: "mongodb tutorial" } })
AReturns all documents containing both words 'mongodb' and 'tutorial' anywhere in the content field.
BReturns documents containing the exact phrase 'mongodb tutorial' only.
CReturns documents only if the content field starts with 'mongodb tutorial'.
DReturns documents containing either 'mongodb' or 'tutorial' but not both.
Attempts:
2 left
💡 Hint
Think about how MongoDB text search matches words, not exact phrases by default.
📝 Syntax
intermediate
2:00remaining
Identify the correct syntax to create a text index
Which of the following commands correctly creates a text index on the title and description fields of a MongoDB collection named 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
Remember that the index type is specified as a string.
optimization
advanced
2:00remaining
Optimize text search for multiple languages
You want to create a text index on the comments field of a collection reviews that supports English and Spanish languages for better search relevance. Which command achieves this?
Adb.reviews.createIndex({ comments: "text" }, { default_language: "spanish" })
Bdb.reviews.createIndex({ comments: "text" }, { weights: { comments: 1 }, default_language: "spanish" })
Cdb.reviews.createIndex({ comments: "text" }, { language_override: "spanish" })
Ddb.reviews.createIndex({ comments: "text" }, { default_language: "english" })
Attempts:
2 left
💡 Hint
MongoDB text indexes support one default language per index for stemming and stop words.
🔧 Debug
advanced
2:00remaining
Why does this text search query return no results?
You created a text index on the summary field of the books collection. You run this query but get no results: db.books.find({ $text: { $search: "adventure" } }). What is the most likely reason?
AThe <code>$search</code> operator requires exact phrase matching with quotes.
BThe text index was created on a different field, not on <code>summary</code>.
CThe collection has no documents with the word 'adventure' in the <code>summary</code> field.
DText search queries require specifying the language in the query.
Attempts:
2 left
💡 Hint
Check which fields have the text index.
🧠 Conceptual
expert
2:00remaining
Understanding text score sorting in MongoDB
You run this query on a collection posts with a text index on content:
db.posts.find({ $text: { $search: "database" } }, { score: { $meta: "textScore" } }).sort({ score: { $meta: "textScore" } })
What does the score field represent in the output documents?
AThe timestamp when the document was last updated.
BThe number of times the word 'database' appears in the content field.
CA numeric value representing how well the document matches the search terms; higher means better match.
DThe length of the content field in characters.
Attempts:
2 left
💡 Hint
Text score is a relevance measure computed by MongoDB.