MongoDB & NoSQL (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What is a Partial Index vs a Sparse Index in MongoDB?
senior LevelMost AskedCode ExampleDatabases & SQL
In Simple Words:
- Sparse Index: Only indexes documents that contain the indexed field (skips documents where the field does not exist).
- Partial Index (Modern Replacement): Indexes only documents that satisfy an explicit filter expression:
Saves massive disk and RAM capacity by excluding millions of archived/cancelled orders from the index!javascript
db.orders.createIndex( { orderId: 1 }, { partialFilterExpression: { status: "ACTIVE" } } );