Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 or -1 instead of "text" for the index type.
Using "index" which is not a valid index type.
✗ Incorrect
To enable text search on a field, you create a text index by specifying the value "text" for that field.
2fill in blank
mediumComplete the code to perform a text search for the word 'coffee' in the 'products' collection.
MongoDB
db.products.find({ $[1]: { $search: "coffee" } }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $match or $search instead of $text.
Omitting the dollar sign before the operator.
✗ Incorrect
The $text operator is used to perform text search queries in MongoDB.
3fill in blank
hardFix the error in the query to search for 'tea' and sort results by text score.
MongoDB
db.products.find({ $text: { $search: "tea" } }).sort({ [1]: { $meta: "textScore" } }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'textScore' or other incorrect field names instead of 'score'.
Not using $meta operator inside sort.
✗ Incorrect
When sorting by text score, the field name must be the same as used in projection, typically 'score'.
4fill in blank
hardFill both blanks to project the text score and sort results by it.
MongoDB
db.products.find({ $text: { $search: "juice" } }, { [1]: { $meta: "textScore" } }).sort({ [2]: { $meta: "textScore" } }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different field names for projection and sorting.
Using fields unrelated to text score like 'rating' or 'popularity'.
✗ Incorrect
The text score field is named 'score' and must be used both in projection and sorting with $meta "textScore".
5fill in blank
hardFill all three blanks to create a text index on 'title' and 'content', search for 'summer', and project the text score.
MongoDB
db.articles.createIndex({ [1]: "text", [2]: "text" });
db.articles.find({ $text: { $search: "summer" } }, { [3]: { $meta: "textScore" } }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names for index or projection.
Forgetting to specify "text" as the index type.
✗ Incorrect
Create a text index on 'title' and 'content' fields, then project the text score as 'score' in the find query.