Recall & Review
beginner
What is a sparse index in MongoDB?
A sparse index only includes documents in the index if the indexed field exists. Documents without the field are not indexed.
Click to reveal answer
beginner
How does a sparse index differ from a regular index?
A regular index includes all documents, even if the indexed field is missing or null. A sparse index skips documents missing the field.
Click to reveal answer
intermediate
Why use a sparse index?
To save space and improve performance when many documents do not have the indexed field, because only documents with the field are indexed.
Click to reveal answer
intermediate
Can a sparse index be used with unique constraints?
Yes, but only documents with the indexed field are checked for uniqueness. Documents without the field can have duplicates.
Click to reveal answer
beginner
How do you create a sparse index in MongoDB?
Use the createIndex method with the option { sparse: true }, for example: db.collection.createIndex({ field: 1 }, { sparse: true })
Click to reveal answer
What does a sparse index in MongoDB exclude?
✗ Incorrect
Sparse indexes exclude documents where the indexed field does not exist.
Which option is used to create a sparse index in MongoDB?
✗ Incorrect
The option { sparse: true } tells MongoDB to create a sparse index.
Can a sparse index enforce uniqueness on documents missing the indexed field?
✗ Incorrect
Sparse unique indexes enforce uniqueness only on documents that have the indexed field.
What is a benefit of using a sparse index?
✗ Incorrect
Sparse indexes save space by indexing only documents that contain the indexed field.
If a document does not have the indexed field, how does a sparse index treat it?
✗ Incorrect
Documents missing the indexed field are excluded from a sparse index.
Explain what a sparse index is and when you would use it in MongoDB.
Think about indexing only documents that have a certain field.
You got /4 concepts.
Describe how uniqueness works with sparse indexes in MongoDB.
Consider how missing fields affect unique constraints.
You got /3 concepts.