0
0
MongoDBquery~10 mins

Text indexes for search in MongoDB - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a text index on the 'description' field.

MongoDB
db.products.createIndex({ description: [1] })
Drag options to blanks, or click blank then click option'
A1
B-1
C"index"
D"text"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or -1 creates a normal ascending or descending index, not a text index.
Using "index" is not a valid index type.
2fill in blank
medium

Complete the code to search for documents containing the word 'coffee' using the text index.

MongoDB
db.products.find({ $[1]: { $search: "coffee" } })
Drag options to blanks, or click blank then click option'
Amatch
Btext
Csearch
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using $match or $search instead of $text causes errors.
Using $query is not valid for text search.
3fill in blank
hard

Fix the error in the code to create a text index on both 'title' and 'content' fields.

MongoDB
db.articles.createIndex({ title: [1], content: [1] })
Drag options to blanks, or click blank then click option'
A-1
B1
C"text"
D"index"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or -1 for text indexes is incorrect.
Using different types for each field causes errors.
4fill in blank
hard

Fill both blanks to perform a text search for 'mongodb' and sort results by text score.

MongoDB
db.blog.find({ $[1]: { $search: "mongodb" } }, { score: { $meta: [2] } }).sort({ score: { $meta: "textScore" } })
Drag options to blanks, or click blank then click option'
Atext
BtextScore
Cscore
DsearchScore
Attempts:
3 left
💡 Hint
Common Mistakes
Using $search instead of $text causes errors.
Using incorrect meta values like "score" or "searchScore" fails.
5fill in blank
hard

Fill all three blanks to create a text index on 'summary', search for 'database', and project the text score.

MongoDB
db.reviews.createIndex({ [1]: [2] });
db.reviews.find({ $[3]: { $search: "database" } }, { score: { $meta: "textScore" } })
Drag options to blanks, or click blank then click option'
Asummary
B"text"
Ctext
Dcontent
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names or index types.
Using $search instead of $text for the query.