0
0
MongoDBquery~10 mins

Compound index and field order 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 compound index on fields 'name' and 'age'.

MongoDB
db.users.createIndex({ name: 1, [1]: 1 })
Drag options to blanks, or click blank then click option'
Aage
Bemail
Caddress
Dphone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field not intended for the index like 'email'.
Forgetting to specify the field name.
2fill in blank
medium

Complete the code to find documents where 'name' is 'Alice' and 'age' is 30 using the compound index.

MongoDB
db.users.find({ name: 'Alice', [1]: 30 })
Drag options to blanks, or click blank then click option'
Aphone
Baddress
Cemail
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field not in the index for filtering.
Misspelling the field name.
3fill in blank
hard

Fix the error in the compound index creation by choosing the correct field order.

MongoDB
db.users.createIndex({ [1]: 1, name: 1 })
Drag options to blanks, or click blank then click option'
Aaddress
Bemail
Cage
Dphone
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping field order incorrectly.
Using fields not intended for the index.
4fill in blank
hard

Fill both blanks to create a compound index on 'city' ascending and 'score' descending.

MongoDB
db.players.createIndex({ [1]: 1, [2]: -1 })
Drag options to blanks, or click blank then click option'
Acity
Bscore
Cage
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up field names.
Using wrong sort order values.
5fill in blank
hard

Fill all three blanks to create a compound index on 'category' ascending, 'price' ascending, and 'rating' descending.

MongoDB
db.products.createIndex({ [1]: 1, [2]: 1, [3]: -1 })
Drag options to blanks, or click blank then click option'
Acategory
Bprice
Crating
Dstock
Attempts:
3 left
💡 Hint
Common Mistakes
Incorrect field order.
Wrong sort directions.