Multikey Indexes on Arrays & Architecture Restrictions
When an index includes an array field, MongoDB creates a Multikey index where each array element receives its own B-Tree entry, introducing critical compound index constraints.
Index Traversal & IXSCAN vs COLLSCAN Execution Plan
Topic 23: Multikey Indexes on Arrays & Architecture Restrictions
{
"queryPlanner": {
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"indexName": "role_1_country_1",
"isMultiKey": false,
"direction": "forward"
}
}
},
"executionStats": {
"nReturned": 2,
"executionTimeMillis": 1,
"totalKeysExamined": 2,
"totalDocsExamined": 2
}
}1. Formal Definition & Architecture Intent
A Multikey Index is an index where one or more indexed fields is an array. For each document, MongoDB creates a separate index key for every element in the array. In a compound multikey index, at most ONE field can be an array to prevent exponential combinatorial index entry explosions.
2. Explain Like I'm 5 (Mental Model)
If a book has 5 tags, a multikey index creates 5 separate bookmarks pointing to that same book. If you tried to index 5 tags and 5 authors together combinatorially, you would have to write 25 bookmarks for just one book!
3. Real-World Architecture Example
Indexing user tags for instant keyword searches.
{
"_id": "u1",
"name": "Dev Dave",
"tags": [
"backend",
"go",
"k8s"
]
}An index on { tags: 1 } creates three index keys pointing to document u1.
4. MQL Implementation Steps
db.users.createIndex({ tags: 1 });db.users.find({ tags: "k8s" });5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Single Array Limit | A compound index cannot contain more than one array field (prevents Cartesian product explosion). |
| Multikey Flag | Once a collection receives a single document with an array in an indexed field, the index is permanently marked multikey. |
6. Deep Dive: Cartesian Product Prevention Rule
If MongoDB allowed compound indexes on two array fields (e.g. tags: [5 items] and categories: [5 items]), a single document would generate 5 x 5 = 25 B-Tree entries. If both arrays had 100 items, one document would generate 10,000 index entries, destroying write throughput and exhausting RAM.
// ❌ Attempting to create compound index on two arrays throws error:
// db.products.createIndex({ tags: 1, categories: 1 });
// MongoServerError: cannot index parallel arrays [tags] [categories]7. Architectural Trade-offs
- •Enables fast indexed lookups for array membership and tags queries
- •Supports element matching ($elemMatch) on arrays of nested subdocuments
- •Seamlessly handled by the same syntax as scalar indexes
- •Documents with large arrays generate many B-Tree entries, increasing write latency and index size
- •Cannot index two parallel array fields in the same compound index
8. Technical Interview Questions
To prevent a Cartesian product explosion of index entries. If a document contained two arrays with N and M elements respectively, indexing both in a compound index would generate N x M index keys for that single document, causing severe write amplification, index bloat, and memory exhaustion.
9. Staff-Level Ready Answer
"When an indexed field contains an array, MongoDB creates a Multikey index where each array item generates a separate B-Tree entry. To prevent combinatorial key explosion, MongoDB strictly forbids indexing more than one array field in a compound index."
10. Quick Knowledge Check
What error does MongoDB throw if you attempt to insert a document with two arrays into a compound index covering both fields?