0
0
MongoDBquery~10 mins

Why indexes are critical for performance in MongoDB - Test Your Understanding

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

Complete the code to create an index on the 'name' field in MongoDB.

MongoDB
db.collection.createIndex({ name: [1] })
Drag options to blanks, or click blank then click option'
A1
Bfalse
Ctrue
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using -1 instead of 1 for ascending index.
Using true or false which are invalid values here.
2fill in blank
medium

Complete the query to find documents where 'age' is greater than 30 using an index.

MongoDB
db.collection.find({ age: { [1]: 30 } })
Drag options to blanks, or click blank then click option'
A$lt
B$gt
C$eq
D$ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lt which means less than.
Using $eq which means equals.
3fill in blank
hard

Fix the error in the index creation code to ensure it creates a compound index on 'age' ascending and 'score' descending.

MongoDB
db.collection.createIndex({ age: [1], score: [2] })
Drag options to blanks, or click blank then click option'
A1
Btrue
C-1
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using true or false instead of 1 or -1.
Using the same order for both fields.
4fill in blank
hard

Fill both blanks to write a query that uses an index to find documents where 'status' is 'active' and 'points' are at least 50.

MongoDB
db.collection.find({ status: [1], points: { [2]: 50 } })
Drag options to blanks, or click blank then click option'
A"active"
B$gte
C$lte
D"inactive"
Attempts:
3 left
💡 Hint
Common Mistakes
Using $lte instead of $gte for points.
Using 'inactive' instead of 'active' for status.
5fill in blank
hard

Fill all three blanks to create a text index on 'title' and 'description' fields and then query documents matching 'mongodb'.

MongoDB
db.collection.createIndex({ [1]: "text", [2]: "text" })
db.collection.find({ $text: { $search: [3] } })
Drag options to blanks, or click blank then click option'
Atitle
Bdescription
C"mongodb"
D"database"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names for the index.
Not quoting the search string.