Course contentsShow
← Databases & SQL
Lesson 1184 of 1,95311. NoSQL and Document ModelsFree lesson

Indexing in Document Stores

Create efficient indexes on deep-nested fields and array values.

In this lesson, you will learn how to optimize query performance by creating indexes on deep-nested object fields and complex array structures within your document store.

The Challenge of Deep Nesting

In a document store, your data might look like this:

{
  "order_id": 101,
  "metadata": {
    "shipping": {
      "courier": "ExpressShip",
      "priority": "High"
    }
  },
  "tags": ["fragile", "urgent", "international"]
}

If you frequently query for all orders handled by a specific courier or those marked with a specific tag, a basic document-level index won’t suffice. To make these queries efficient, you must target the specific paths within the document.

Indexing Nested Objects

When querying deep-nested fields, you use Dot Notation to define the index path. By indexing metadata.shipping.courier, the database engine creates a B-tree structure where the keys are the courier names. This allows the engine to jump directly to the relevant documents without scanning the entire collection.

Indexing Array Values (Multikey Indexes)

Indexing an array is a unique operation known as a Multikey Index. When you create an index on the tags field, the database automatically treats the array as a list of independent entries. If a document has three tags, the index stores three separate entries for that single document, all pointing back to the same parent.

This is incredibly powerful but comes with a storage trade-off: your index size grows proportionally to the number of elements in your arrays. You must be careful not to index massive arrays, as the write overhead can impact the performance of your INSERT and UPDATE operations.

Best Practices

  1. Be Specific: Only index the exact nested path you need. Over-indexing deep paths increases storage costs significantly.
  2. Watch Cardinality: For multikey indexes, ensure the values in your arrays have high cardinality (many unique values) to prevent the index from becoming inefficient due to excessive duplication.
  3. Avoid Deep Nesting: If your data requires deeply nested indexing, consider if your document model is becoming too complex for your read patterns.

Key Takeaway: Use dot notation to reach deep fields and multikey indexing for arrays to transform slow, full-collection scans into high-speed, targeted lookups.