0
0
MongoDBquery~20 mins

createIndex method in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this createIndex command do?
Consider the following MongoDB command:
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 })
ACreates a descending index on the email field but allows duplicate email values.
BRemoves all documents with duplicate email values from the collection.
CCreates a text index on the email field for full-text search.
DCreates an ascending index on the email field and enforces unique email values in the collection.
Attempts:
2 left
💡 Hint
Think about what the 1 and unique: true mean in the index creation.
query_result
intermediate
2:00remaining
What is the output of this createIndex command?
Given the command:
db.orders.createIndex({ orderDate: -1 })

What kind of index is created on the orderDate field?
MongoDB
db.orders.createIndex({ orderDate: -1 })
AA descending index on orderDate.
BA hashed index on orderDate.
CAn ascending index on orderDate.
DA compound index on orderDate and another field.
Attempts:
2 left
💡 Hint
The value -1 indicates the index order direction.
📝 Syntax
advanced
2: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):
Adb.collection.createIndex({ firstName: 'asc', lastName: 'desc' })
Bdb.collection.createIndex([ firstName: 1, lastName: -1 ])
Cdb.collection.createIndex({ firstName: 1, lastName: -1 })
Ddb.collection.createIndex({ firstName => 1, lastName => -1 })
Attempts:
2 left
💡 Hint
Remember the syntax for specifying fields and order in createIndex.
optimization
advanced
2: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?
ACreate a unique index on the status field.
BCreate a hashed index on the status field.
CCreate a sparse index on the status field.
DCreate a multikey index on the status field.
Attempts:
2 left
💡 Hint
Think about how hashed indexes distribute values for equality queries.
🔧 Debug
expert
3:00remaining
Why does this createIndex command fail?
You run this command:
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 })
ABecause there are multiple documents with the same price value violating the unique constraint.
BBecause the createIndex method requires a second field for uniqueness.
CBecause unique indexes cannot be created on numeric fields.
DBecause the price field cannot be indexed in ascending order.
Attempts:
2 left
💡 Hint
Unique indexes require all indexed values to be different.