Docs Menu
Docs Home
/ / /
PyMongo
/ /

Multikey Indexes

On this page

  • Overview
  • Sample Data
  • Create a Multikey Index

Multikey indexes are indexes that improve performance for queries that specify a field with an index that contains an array value. You can define a multikey index by using the same syntax as a single field or compound index.

The examples in this guide use the sample_mflix.movies collection from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the Get Started with PyMongo.

The following example creates a multikey index on the cast field:

result = movies.create_index("cast")

The following is an example of a query that uses the index created in the preceding code example:

query = { "cast": "Viola Davis" }
cursor = movies.find(query)

Multikey indexes behave differently from other indexes in terms of query coverage, index bound computation, and sort behavior. To learn more about multikey indexes, including a discussion of their behavior and limitations, see the Multikey Indexes guide in the MongoDB Server manual.

Back

Compound