📖 Scenario: You are managing a MongoDB collection that stores user profiles. Not all users provide their phone numbers, so the phone field is missing in some documents. To optimize queries that search by phone number without indexing documents missing this field, you want to create a sparse index.
🎯 Goal: Build a MongoDB sparse index on the phone field to efficiently query users who have provided their phone numbers.
📋 What You'll Learn
Create a MongoDB collection named users with specific user documents.
Add a sparse index on the phone field.
Write a query to find users with a specific phone number using the sparse index.
Ensure the sparse index excludes documents without the phone field.
💡 Why This Matters
🌍 Real World
Sparse indexes help optimize database queries when some documents do not have certain fields, common in user profiles or optional data scenarios.
💼 Career
Understanding sparse indexes is important for database administrators and backend developers to improve query performance and storage efficiency.
Progress0 / 4 steps
1
DATA SETUP: Create the users collection with sample documents
Insert these exact documents into a MongoDB collection called users: { name: "Alice", age: 28, phone: "123-456-7890" }, { name: "Bob", age: 34 }, { name: "Charlie", age: 25, phone: "987-654-3210" }, and { name: "Diana", age: 30 }.
MongoDB
Hint
Use db.users.insertMany() with an array of documents.
2
CONFIGURATION: Define the sparse index on the phone field
Create a sparse index on the phone field in the users collection using db.users.createIndex() with the option { sparse: true }.
MongoDB
Hint
Use db.users.createIndex({ phone: 1 }, { sparse: true }) to create the sparse index.
3
CORE LOGIC: Query users with a specific phone number
Write a query using db.users.find() to find the user document where the phone field equals "123-456-7890".
MongoDB
Hint
Use db.users.find({ phone: "123-456-7890" }) to query the user.
4
COMPLETION: Verify the sparse index excludes documents without phone
Add a command to list all indexes on the users collection using db.users.getIndexes() to confirm the sparse index exists.
MongoDB
Hint
Use db.users.getIndexes() to see all indexes on the collection.
Practice
(1/5)
1. What is the main purpose of a sparse index in MongoDB?
easy
A. To index only documents that contain the indexed field
B. To index all documents regardless of field presence
C. To create a backup of the database
D. To speed up writes by disabling indexing
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 A
Quick Check:
Sparse index = indexes only documents with the field [OK]
Hint: Sparse indexes skip documents missing the field [OK]
Common Mistakes:
Thinking sparse indexes index all documents
Confusing sparse indexes with backups
Assuming sparse indexes disable indexing
2. Which of the following is the correct way to create a sparse index on the field email in MongoDB?
easy
A. db.collection.createIndex({ email: 1 }, { unique: true })
B. db.collection.createIndex({ email: 1 }, { background: true })
C. db.collection.createIndex({ email: 1 }, { sparse: true })
D. db.collection.createIndex({ email: 1 }, { expireAfterSeconds: 3600 })
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.
Hint: Use { sparse: true } option to create sparse indexes [OK]
Common Mistakes:
Using unique instead of sparse option
Confusing background with sparse
Using expireAfterSeconds for sparse index
3. Given a collection with documents:
{ _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 } })?
medium
A. Documents with _id 1 and 3 only
B. Documents with _id 1, 2, and 3
C. Documents with _id 2 only
D. No documents returned
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 A
Quick Check:
Sparse index excludes docs missing field, so only _id 1 and 3 match [OK]
Hint: Sparse index excludes docs missing field, so only those with field match [OK]
Common Mistakes:
Including documents without the indexed field
Assuming all documents are returned
Confusing sparse index with regular index
4. You created a sparse index on phone field but your queries on phone are slow. What is a likely cause?
medium
A. Sparse indexes index all documents regardless of field presence
B. Sparse indexes always slow down queries
C. You forgot to create the index
D. Many documents do not have the phone field, so index is small and not used
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 the phone field, so index is small and not used -> Option D
Quick Check:
Sparse index small if field missing in many docs, may not speed queries [OK]
Hint: Sparse index small if many docs lack field, may not improve speed [OK]
Common Mistakes:
Assuming sparse indexes always slow queries
Forgetting to create the index
Thinking sparse indexes index all documents
5. You want to create an index on the status field, but only for documents where status exists and is not null. Which MongoDB index type should you use and why?
hard
A. TTL index, because it removes documents after a time
B. Sparse index, because it indexes only documents with the field present
C. Regular index, because it indexes all documents regardless of field
D. Unique index, because it enforces unique values
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 B
Quick Check:
Use sparse index to index only docs with the field [OK]
Hint: Use sparse index to index only documents with the field present [OK]
Common Mistakes:
Choosing regular index which indexes all documents
Confusing TTL or unique indexes with sparse
Not understanding sparse index skips missing fields
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.