Learning MongoDB

0% completed

Previous
Next
Multikey Indexes

Multikey indexes in MongoDB are used to index fields that contain arrays. These indexes allow MongoDB to index each element of the array, which enables efficient querying and sorting based on array values. Multikey indexes are particularly useful for applications that store lists or sets of values within a single field.

Key Points

  • Indexes array elements: Multikey indexes allow for efficient querying of documents based on elements within an array.
  • Supports complex queries: These indexes are useful for queries that need to match, sort, or aggregate data based on array contents.
  • Automatic creation: MongoDB automatically converts an index on an array field into a multikey index.

Syntax

db.collection.createIndex({ <arrayField>: <type> }, { <option1>: <value1>, <option2>: <value2>, ... })
  • arrayField: The array field to be indexed.
  • type: The type of index, usually 1 for ascending order or -1 for descending order.
  • option1, option2: Optional parameters to customize the index behavior (e.g., unique, background).

Example Setup

Let's insert some documents into the books collection:

db.books.insertMany([ { title: "To Kill a Mockingbird", authors: ["Harper Lee"], genres: ["Fiction", "Classic"], published: 1960 }, { title: "1984", authors: ["George Orwell"], genres: ["Dystopian", "Science Fiction"], published: 1949 }, { title: "Moby Dick", authors: ["Herman Melville"], genres: ["Adventure", "Classic"], published: 1851 }, { title: "The Great Gatsby", authors: ["F. Scott Fitzgerald"], genres: ["Fiction", "Classic"], published: 1925 }, { title: "War and Peace", authors: ["Leo Tolstoy"], genres: ["Historical", "Classic"], published: 1869 } ])

Index Creation Example

Create a multikey index on the genres field:

db.books.createIndex({ genres: 1 })

Explanation:

  • db.books.createIndex({ genres: 1 }): This command creates a multikey index on the genres array field. This index will speed up queries that filter or sort by the elements of the genres array.

Querying Using the Indexes

Example 1: Query by Array Element

db.books.find({ genres: "Classic" })

Explanation:

  • This query uses the multikey index on the genres field to quickly locate documents where one of the genres is "Classic".

Example 2: Query with Multiple Array Elements

db.books.find({ genres: { $all: ["Fiction", "Classic"] } })

Explanation:

  • This query uses the multikey index to find documents where the genres array contains both "Fiction" and "Classic".

Considerations

  1. Index Limitations: MongoDB limits the number of indexed array elements to 1024 per document for performance reasons.
  2. Index Overhead: Multikey indexes can consume more disk space and increase the overhead on write operations compared to single field indexes.
  3. Nested Arrays: Multikey indexes cannot be created on fields that contain nested arrays.

Multikey indexes in MongoDB are essential for optimizing queries that involve array fields. By indexing each element of an array, MongoDB can efficiently retrieve and sort documents based on array contents.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next