0
0
MongoDBquery~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
How do indexes improve query speed in MongoDB?

Imagine you have a huge phone book and you want to find a person's phone number. Without an index, you might have to look at every page. How does an index help MongoDB find data faster?

AIndexes compress data to make it smaller and faster to read.
BIndexes store all data in memory so queries never access the disk.
CIndexes let MongoDB jump directly to the data location without scanning all documents.
DIndexes duplicate all data to speed up queries.
Attempts:
2 left
💡 Hint

Think about how a book's index helps you find a topic quickly.

query_result
intermediate
1:30remaining
What is the output count of a query with and without an index?

Given a collection users with 100,000 documents, 10,000 have age: 30. You run this query:

db.users.find({age: 30}).count()

What will be the count returned?

A10000
B0
C100000
DError: no index found
Attempts:
2 left
💡 Hint

The count is about how many documents match, not how fast the query runs.

📝 Syntax
advanced
1:30remaining
Which index creation command is correct for fast queries on 'email' field?

You want to create an index on the email field in the users collection. Which command is correct?

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

Indexes need a field name and sort order inside an object.

optimization
advanced
2:00remaining
Which index type is best for queries filtering on multiple fields?

You often query orders collection filtering by customerId and status. Which index improves performance best?

AA text index on customerId and status
BA compound index on {customerId: 1, status: 1}
CSeparate single-field indexes on customerId and status
DNo index, just scan all documents
Attempts:
2 left
💡 Hint

Think about how combining fields in one index helps multi-field queries.

🔧 Debug
expert
2:30remaining
Why does this query run slowly despite an index on 'username'?

Collection users has an index on username. This query is slow:

db.users.find({username: /john/i})

Why?

ARegex with case-insensitive flag prevents index use
BIndex on username is corrupted
CQuery syntax is invalid
DMongoDB does not support indexes on strings
Attempts:
2 left
💡 Hint

Think about how indexes work with regular expressions.