Skip to content
LESSON 23 / 50Intermediateindexing

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.

Interactive Engine Simulator

Index Traversal & IXSCAN vs COLLSCAN Execution Plan

Topic 23: Multikey Indexes on Arrays & Architecture Restrictions

Execution Stage
IXSCAN + FETCH
B-Tree leaf seek
Documents Examined
2
O(log N) lookup
Keys Examined
2
Index keys inspected
Query Latency
0.3 ms
WiredTiger execution time
Explain Plan Output Summary:
{
  "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.

Sample BSON Documents:
Collection: db.users
{
  "_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

Step 1: Create multikey index on array field
db.users.createIndex({ tags: 1 });
Automatically flagged as multikey in catalog metadata.
Step 2: Query array containment using multikey index
db.users.find({ tags: "k8s" });
Executes an IXSCAN on the "k8s" index key.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Single Array LimitA compound index cannot contain more than one array field (prevents Cartesian product explosion).
Multikey FlagOnce 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]
Production Warning: Never attempt to index two parallel array fields in a single compound index.

7. Architectural Trade-offs

Advantages
  • •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
Disadvantages & Gotchas
  • •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

Q1: Why does MongoDB prohibit creating a compound index on two parallel array fields?

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?