0
0
MongoDBquery~20 mins

When not to index in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Indexing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When should you avoid indexing a field in MongoDB?

Which situation is NOT a good reason to create an index on a MongoDB collection field?

AThe field contains unique values that need to be enforced.
BThe field is frequently updated with new values.
CThe field is often used in query filters to find documents.
DThe field is used in sorting query results.
Attempts:
2 left
💡 Hint

Think about how indexes affect write operations and storage.

query_result
intermediate
2:00remaining
Effect of indexing a low-cardinality field

Consider a MongoDB collection with a field status that only has two possible values: 'active' or 'inactive'. What is the likely effect of creating an index on this status field?

AThe index will greatly improve query speed for filtering by status.
BThe index will cause errors when querying the collection.
CThe index will have little benefit and may slow down writes.
DThe index will reduce the collection size significantly.
Attempts:
2 left
💡 Hint

Think about how selective the index is when the field has few distinct values.

📝 Syntax
advanced
2:00remaining
Identify the incorrect index creation command

Which MongoDB command will cause a syntax error when trying to create an index?

MongoDB
db.users.createIndex({ age: 'ascending' })
Adb.users.createIndex({ age: 'ascending' })
Bdb.users.createIndex({ age: 1 })
C)} 1 :ega {(xednIetaerc.sresu.bd
Ddb.users.createIndex({ age: -1 })
Attempts:
2 left
💡 Hint

Check the valid values for index direction in MongoDB.

optimization
advanced
2:00remaining
Choosing not to index a frequently updated field

You have a MongoDB collection where the lastLogin field is updated every time a user logs in. You also query users by lastLogin date range sometimes. What is the best indexing strategy?

ADo not index <code>lastLogin</code> because frequent updates will slow writes.
BCreate a compound index including <code>lastLogin</code> and <code>userId</code>.
CCreate a single-field index on <code>lastLogin</code> to speed up queries.
DCreate a TTL index on <code>lastLogin</code> to automatically delete old records.
Attempts:
2 left
💡 Hint

Consider the trade-off between read speed and write performance.

🔧 Debug
expert
3:00remaining
Why does this index not improve query performance?

A developer created an index on the category field of a MongoDB collection. The category field contains many repeated values. Queries filtering by category are still slow. What is the most likely reason?

AThe queries do not use the <code>category</code> field in their filter.
BThe index was created with the wrong syntax and is not active.
CThe collection is too small for indexes to have any effect.
DThe index is on a low-cardinality field, so it is not selective enough to speed up queries.
Attempts:
2 left
💡 Hint

Think about how index selectivity affects query speed.