Sparse indexes help speed up searches by only indexing documents that have a specific field. This saves space and makes queries faster when some documents don't have that field.
Sparse indexes in MongoDB
db.collection.createIndex({ field: 1 }, { sparse: true })The sparse: true option tells MongoDB to only index documents where the field exists and is not null.
The 1 means ascending order for the index.
email field in the users collection. Only users with an email will be indexed.db.users.createIndex({ email: 1 }, { sparse: true })discount field in the products collection. Only products with a discount will be indexed.db.products.createIndex({ discount: 1 }, { sparse: true })This example inserts items where some have a price field and some do not. Then it creates a sparse index on price. Finally, it finds and sorts items that have a price.
db.inventory.insertMany([
{ item: "apple", category: "fruit" },
{ item: "banana", category: "fruit", price: 1 },
{ item: "carrot", category: "vegetable", price: 2 },
{ item: "donut" }
])
// Create sparse index on price
db.inventory.createIndex({ price: 1 }, { sparse: true })
// Find all items with price
db.inventory.find({ price: { $exists: true } }).sort({ price: 1 })Sparse indexes do not include documents where the indexed field is missing or null.
Using sparse indexes can reduce index size and improve query speed for fields not present in all documents.
Be careful: queries that look for documents without the field will not use the sparse index.
Sparse indexes only index documents that have the indexed field.
This saves space and speeds up queries on fields that are not in every document.
Use sparse indexes when many documents might not have the field you want to index.
Practice
sparse index in MongoDB?Solution
Step 1: Understand sparse index behavior
Sparse indexes only include documents that have the indexed field present.Step 2: Compare with other index types
Unlike regular indexes, sparse indexes skip documents missing the field, saving space and improving query speed on sparse data.Final Answer:
To index only documents that contain the indexed field -> Option AQuick Check:
Sparse index = indexes only documents with the field [OK]
- Thinking sparse indexes index all documents
- Confusing sparse indexes with backups
- Assuming sparse indexes disable indexing
email in MongoDB?Solution
Step 1: Identify sparse index syntax
The option { sparse: true } is used to create a sparse index in MongoDB.Step 2: Verify the correct command
The command db.collection.createIndex({ email: 1 }, { sparse: true }) correctly creates a sparse index on the email field.Final Answer:
db.collection.createIndex({ email: 1 }, { sparse: true }) -> Option CQuick Check:
Sparse index creation uses { sparse: true } [OK]
- Using unique instead of sparse option
- Confusing background with sparse
- Using expireAfterSeconds for sparse index
{ _id: 1, name: "Alice", age: 30 }{ _id: 2, name: "Bob" }{ _id: 3, name: "Charlie", age: 25 }and a sparse index on
age, what documents will be returned by db.collection.find({ age: { $gte: 25 } })?Solution
Step 1: Understand sparse index effect on query
The sparse index on age only indexes documents that have the age field. Document with _id 2 has no age field, so it is not indexed.Step 2: Evaluate the query filter
The query { age: { $gte: 25 } } matches documents where age is 25 or more. Documents with _id 1 (age 30) and _id 3 (age 25) match. Document _id 2 is excluded because it has no age field.Final Answer:
Documents with _id 1 and 3 only -> Option AQuick Check:
Sparse index excludes docs missing field, so only _id 1 and 3 match [OK]
- Including documents without the indexed field
- Assuming all documents are returned
- Confusing sparse index with regular index
phone field but your queries on phone are slow. What is a likely cause?Solution
Step 1: Analyze sparse index behavior
Sparse indexes only index documents with the field. If many documents lack the phone field, the index is small.Step 2: Understand query performance impact
If the index is small or not selective, MongoDB may choose not to use it, causing slower queries.Final Answer:
Many documents do not have thephonefield, so index is small and not used -> Option DQuick Check:
Sparse index small if field missing in many docs, may not speed queries [OK]
- Assuming sparse indexes always slow queries
- Forgetting to create the index
- Thinking sparse indexes index all documents
status field, but only for documents where status exists and is not null. Which MongoDB index type should you use and why?Solution
Step 1: Identify index requirement
You want to index only documents where status exists and is not null, skipping documents without status.Step 2: Match index type to requirement
Sparse indexes index only documents that have the indexed field, which fits the requirement perfectly.Final Answer:
Sparse index, because it indexes only documents with the field present -> Option BQuick Check:
Use sparse index to index only docs with the field [OK]
- Choosing regular index which indexes all documents
- Confusing TTL or unique indexes with sparse
- Not understanding sparse index skips missing fields
Ready to prove it?
Pass the test and this pattern moves unknown
