0
0
MongoDBquery~20 mins

How MongoDB indexes work (B-tree mental model) - Practice Exercises

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

Imagine you have a phone book sorted by last names. How does MongoDB's B-tree index help find a specific last name faster compared to scanning every entry?

AIt organizes data in a tree structure allowing quick jumps to the right section instead of checking every entry.
BIt stores all data in a single list and searches linearly from start to end.
CIt duplicates the entire collection to speed up queries.
DIt randomly picks entries until it finds the match.
Attempts:
2 left
💡 Hint

Think about how a dictionary helps you find words quickly by jumping to the right letter section.

query_result
intermediate
2:00remaining
What is the output count of documents matched using an indexed field?

Given a MongoDB collection users with an index on age, what will be the count result of this query?

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

Assume the collection has 100 documents with ages ranging from 1 to 100.

A30
B0
C100
D70
Attempts:
2 left
💡 Hint

Count how many ages are greater than 30 in the range 1 to 100.

📝 Syntax
advanced
2:00remaining
Which MongoDB index creation command is syntactically correct for a compound B-tree index?

Choose the correct command to create a compound index on lastName ascending and age descending fields.

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

Remember MongoDB uses JSON-like objects with field names and 1 or -1 for ascending/descending.

optimization
advanced
2:00remaining
Which index choice optimizes queries filtering by both category and price fields?

You often query a products collection filtering by category and price. Which index will speed up these queries best?

AA text index on category and price
BSeparate single-field indexes on category and price
CA compound index on { category: 1, price: 1 }
DNo index, just scan the collection
Attempts:
2 left
💡 Hint

Think about how a single index can cover multiple fields in order.

🔧 Debug
expert
2:00remaining
Why does this MongoDB query not use the index as expected?

Given an index on username, why does this query not use the index?

db.users.find({ username: { $regex: 'admin' } })
ABecause the regex is a string pattern and MongoDB cannot use indexes with regex queries unless they are simple prefixes.
BBecause regex queries with anchors (^) always ignore indexes in MongoDB.
CBecause the regex is case sensitive and the index is case insensitive.
DBecause regex with ^ can use the index, so this query should use it.
Attempts:
2 left
💡 Hint

Consider how MongoDB uses indexes with regex patterns.