0
0
MongoDBquery~20 mins

Sparse indexes in MongoDB - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sparse Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of sparse index on documents without the indexed field

Consider a MongoDB collection with documents where some have the field age and others do not. A sparse index is created on the age field.

What will be the result of the following query?

db.collection.find({ age: { $exists: false } }).hint({ age: 1 })

Assuming the sparse index is used as a hint, what will the query return?

AIt returns no documents because sparse indexes exclude documents without the indexed field.
BIt returns all documents where the <code>age</code> field does not exist.
CIt returns all documents regardless of the <code>age</code> field existence.
DIt causes a query error because sparse indexes cannot be used with $exists queries.
Attempts:
2 left
💡 Hint

Think about what documents are included in a sparse index.

🧠 Conceptual
intermediate
1:30remaining
Purpose of sparse indexes in MongoDB

What is the main purpose of creating a sparse index in MongoDB?

ATo index all documents including those with null or missing values for the indexed field.
BTo automatically update the index when documents are deleted.
CTo create a unique index that enforces uniqueness only on documents with the indexed field.
DTo index only documents that contain the indexed field, saving space and improving performance for sparse data.
Attempts:
2 left
💡 Hint

Think about how sparse data affects indexing.

📝 Syntax
advanced
1:30remaining
Correct syntax to create a sparse index

Which of the following commands correctly creates a sparse index on the email field in a MongoDB collection?

Adb.collection.createIndex({ email: 1 }, { isSparse: true })
Bdb.collection.createIndex({ email: 1 }, { sparseIndex: true })
Cdb.collection.createIndex({ email: 1 }, { sparse: true })
Ddb.collection.createIndex({ email: 1 }, { sparse: 'yes' })
Attempts:
2 left
💡 Hint

Check the exact option name and value type for sparse indexes.

optimization
advanced
2:00remaining
When to prefer sparse indexes over regular indexes

You have a MongoDB collection where only 10% of documents have the phoneNumber field. You want to index this field to speed up queries filtering by phoneNumber.

Which indexing strategy is best to optimize storage and query performance?

ACreate a sparse index on <code>phoneNumber</code> to index only documents that have this field.
BCreate a regular index on <code>phoneNumber</code> to index all documents regardless of field presence.
CCreate a compound index including <code>phoneNumber</code> and another field to cover more queries.
DDo not create any index because sparse fields are not suitable for indexing.
Attempts:
2 left
💡 Hint

Consider how many documents have the field and how sparse indexes work.

🔧 Debug
expert
2:30remaining
Unexpected query result with sparse index

A developer created a sparse index on the status field in a MongoDB collection. They run this query:

db.collection.find({ status: null })

But the query returns documents where status does not exist, even though they expected only documents with status set to null.

Why does this happen?

ABecause the sparse index causes documents with <code>status</code> missing to be indexed as null.
BBecause in MongoDB, querying <code>{ status: null }</code> matches documents where <code>status</code> is null or missing.
CBecause sparse indexes include documents with missing fields as null values.
DBecause the query syntax is invalid and returns all documents.
Attempts:
2 left
💡 Hint

Think about how MongoDB treats null in queries.