Challenge - 5 Problems
MongoDB Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What does this createIndex command do?
Consider the following MongoDB command:
What is the effect of this command on the
db.users.createIndex({ email: 1 }, { unique: true })What is the effect of this command on the
users collection?MongoDB
db.users.createIndex({ email: 1 }, { unique: true })Attempts:
2 left
💡 Hint
Think about what the 1 and unique: true mean in the index creation.
✗ Incorrect
The 1 means ascending order index on the email field. The unique: true option enforces that no two documents can have the same email value.
❓ query_result
intermediate2:00remaining
What is the output of this createIndex command?
Given the command:
What kind of index is created on the
db.orders.createIndex({ orderDate: -1 })What kind of index is created on the
orderDate field?MongoDB
db.orders.createIndex({ orderDate: -1 })Attempts:
2 left
💡 Hint
The value -1 indicates the index order direction.
✗ Incorrect
In MongoDB, -1 means descending order index on the specified field.
📝 Syntax
advanced2:00remaining
Which createIndex command is syntactically correct?
Choose the correct MongoDB createIndex command syntax to create a compound index on fields
firstName (ascending) and lastName (descending):Attempts:
2 left
💡 Hint
Remember the syntax for specifying fields and order in createIndex.
✗ Incorrect
The correct syntax uses an object with field names as keys and 1 or -1 as values for ascending or descending order.
❓ optimization
advanced2:00remaining
Which createIndex option improves query performance for frequent searches on a field with many duplicate values?
You have a collection with a field
status that has only a few distinct values repeated many times. Which index type should you create to optimize queries filtering by status?Attempts:
2 left
💡 Hint
Think about how hashed indexes distribute values for equality queries.
✗ Incorrect
Hashed indexes are good for fields with many duplicate values and optimize equality queries by hashing the field values.
🔧 Debug
expert3:00remaining
Why does this createIndex command fail?
You run this command:
But it fails with an error about duplicate keys. Why?
db.products.createIndex({ price: 1 }, { unique: true })But it fails with an error about duplicate keys. Why?
MongoDB
db.products.createIndex({ price: 1 }, { unique: true })Attempts:
2 left
💡 Hint
Unique indexes require all indexed values to be different.
✗ Incorrect
The error occurs because the collection has duplicate price values, which violates the unique index requirement.