0
0
MongoDBquery~20 mins

Single field index in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Single Field Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of Single Field Index on Query Performance

Consider a MongoDB collection users with a single field index on age. Which query will most likely use this index to improve performance?

MongoDB
db.users.createIndex({ age: 1 })
db.users.find({ age: { $gt: 30 } })
AThe query <code>db.users.find({ age: { $gt: 30 } })</code> uses the index and returns all users older than 30.
BThe query <code>db.users.find({ age: { $exists: true } })</code> uses the index to find all users with an age field.
CThe query <code>db.users.find({ name: 'Alice' })</code> uses the index on age to find users named Alice.
DThe query <code>db.users.find({ age: { $lt: 20 }, name: 'Bob' })</code> uses the index on age and name fields.
Attempts:
2 left
💡 Hint

Indexes help queries that filter on the indexed field.

🧠 Conceptual
intermediate
1:30remaining
Understanding Single Field Index Uniqueness

What is true about a single field index created with { field: 1 } in MongoDB?

AIt creates a non-unique index that speeds up queries filtering by that field.
BIt only indexes documents where the field exists.
CIt creates a unique index by default, preventing duplicate values in the field.
DIt automatically indexes all fields in the collection.
Attempts:
2 left
💡 Hint

Think about default behavior of indexes in MongoDB.

📝 Syntax
advanced
1:30remaining
Correct Syntax to Create a Single Field Index

Which of the following commands correctly creates a single field ascending index on the email field in MongoDB?

Adb.collection.createIndex(['email', 1])
Bdb.collection.createIndex(email: 1)
Cdb.collection.createIndex({ 'email' => 1 })
Ddb.collection.createIndex({ email: 1 })
Attempts:
2 left
💡 Hint

Remember the syntax for specifying fields in an object.

optimization
advanced
2:00remaining
Choosing the Best Index for Query Optimization

You have a collection with fields category and price. You often run queries filtering only by category. Which index is best to optimize these queries?

AA compound index on <code>category</code> and <code>price</code>.
BA single field index on <code>category</code> only.
CA single field index on <code>price</code> only.
DNo index is needed for filtering by <code>category</code>.
Attempts:
2 left
💡 Hint

Indexes should match the fields used in query filters.

🔧 Debug
expert
2:30remaining
Diagnosing Query Performance Issue with Single Field Index

A MongoDB collection has a single field index on status. A query db.orders.find({ status: { $in: ['shipped', 'delivered'] } }) is slow. What is the most likely reason?

AThe <code>$in</code> operator cannot use single field indexes.
BThe index on <code>status</code> is not being used because the query uses an array.
CThe index exists but the query is slow because of missing index statistics or outdated index.
DThe collection is too large and needs a compound index including <code>status</code> and <code>date</code>.
Attempts:
2 left
💡 Hint

Think about index usage and maintenance in MongoDB.