MongoDB - Getting StartedMongoDB - Getting Started1

Multikey Indexes for Arrays



Multikey Indexes for Arrays

In MongoDB, a multikey index is a type of index that allows you to efficiently query fields that contain arrays. Unlike relational databases, MongoDB can store arrays inside documents, and multikey indexes help you search inside those arrays without scanning every document.

Why Are Multikey Indexes Needed?

Let’s say you have a collection of blog posts, and each post has a list of tags. You want to quickly find posts tagged with "mongodb". A multikey index on the tags field allows MongoDB to index each array element separately and retrieve matching documents fast.

Step-by-Step: Create and Use a Multikey Index

Step 1: Insert Sample Data


    db.posts.insertMany([
      { title: "Intro to MongoDB", tags: ["mongodb", "nosql", "database"] },
      { title: "Learn Python", tags: ["python", "programming"] },
      { title: "MongoDB Indexes", tags: ["mongodb", "indexing", "performance"] },
      { title: "Web Development", tags: ["html", "css", "javascript"] }
    ]);
    

Output:

    {
      acknowledged: true,
      insertedIds: [...]
    }
    

Notice how each tags field is an array. MongoDB stores all elements of the array and can index them individually.

Step 2: Create a Multikey Index on the Array Field

Now, we’ll create an index on the tags field.


    db.posts.createIndex({ tags: 1 });
    

Output:

    {
      "createdCollectionAutomatically": false,
      "numIndexesBefore": 1,
      "numIndexesAfter": 2,
      "ok": 1
    }
    

Since tags is an array, MongoDB automatically treats this as a multikey index. It indexes each value inside the array individually.

Step 3: Query with Multikey Index

Let’s run a query to find all posts tagged with "mongodb".


    db.posts.find({ tags: "mongodb" });
    

Output:

    [
      { title: "Intro to MongoDB", tags: ["mongodb", "nosql", "database"] },
      { title: "MongoDB Indexes", tags: ["mongodb", "indexing", "performance"] }
    ]
    

The multikey index allows MongoDB to search efficiently even though tags is an array. Without an index, MongoDB would have to scan every document.

How Does MongoDB Handle Multikey Internally?

For every array field, MongoDB creates a separate index entry for each element in the array. If a document has tags: ["a", "b", "c"], it creates 3 entries in the index — one for each element.

Question:

Q: What happens if I create a multikey index on multiple array fields at once?

A: MongoDB does not support compound multikey indexes on more than one array field. This is because it can result in an exponential number of index entries. However, you can have one array field and one scalar field in a compound index.

Step 4: Using explain() to See the Index in Action

You can verify whether your query is using the index:


    db.posts.find({ tags: "mongodb" }).explain("executionStats");
    

Output:

    {
      ...,
      "executionStats": {
        "totalDocsExamined": 2,
        "totalKeysExamined": 2,
        ...
      },
      "inputStage": {
        "stage": "IXSCAN",
        "indexName": "tags_1",
        ...
      }
    }
    

Notice how "stage": "IXSCAN" confirms that MongoDB used the index instead of scanning all documents.

Best Practices for Multikey Indexes

Summary

Multikey indexes in MongoDB allow efficient searching within array fields by indexing each array element individually. This makes MongoDB highly suitable for applications that store tags, categories, lists, or embedded array data.

In the next topic, we’ll explore how to analyze performance using explain() and further optimize your queries.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M