Docs Menu
Docs Home
/
MongoDB Manual
/ /

Create Indexes to Support Your Queries

On this page

  • About this Task
  • Steps
  • Examples
  • Learn More

An index covers a query when the index contains all of the fields scanned by the query. A covered query scans the index and not the collection, which improves query performance.

Indexes can also partially support queries if a subset of the fields queried are indexed.

A single collection can have a maximum of 64 indexes. However, too many indexes can degrade performance before that limit is reached. For collections with a high write-to-read ratio, indexes can degrade performance because each insert must also update any indexes.

1

To identify common query patterns in your application, use the $queryStats aggregation stage. $queryStats reports metrics for query shapes, which group queries based on shared fields.

2

After you know which fields your application frequently queries, you can create indexes to support queries on those fields. For more information, see Examples.

3

After your application begins using indexes, you can analyze your indexes' effectiveness. To see index statistics and usage, you can:

  • Use the $indexStats aggregation stage.

  • For MongoDB Atlas deployments, view Indexes in the Atlas UI.

Consider deleting unused indexes to improve application performance. For more information, see Remove Unnecessary Indexes.

Repeat this procedure periodically to ensure that your indexes support your current workload.

If your application only queries on a single key in a given collection, then you need to create a single-key index for that collection. For example, you can create an index on category in the product collection:

db.products.createIndex( { category: 1 } )

The preceding index supports this query:

db.products.find( { category: "electronics" } )

If your application performs queries on both a single key and multiple keys, a compound index is more efficient than a single-key index. For example, you can create an index on the category, item, and location fields:

db.products.createIndex( { category: 1, item: 1, location: 1 } )

A compound index supports queries on index prefixes, which are the beginning subsets of indexed fields. For example, the preceding index supports these queries:

db.products.find( { category: "electronics" } )
db.products.find( { category: "electronics", item: "television" } )

For more information and performance considerations on index prefixes, see Index Prefixes.

For data hosted on MongoDB Atlas, you can support full-text search with Atlas Search indexes. To learn more, see Create an Atlas Search Index.

For self-managed (non-Atlas) deployments, MongoDB provides a text index type that supports searching for string content in a collection. To learn more about self-managed text indexes, see Text Indexes on Self-Managed Deployments.

Vector Search Indexes support queries on vector embeddings. To create Vector Search Indexes, see Index Fields for Vector Search.

To use an index for string comparisons, an operation must also specify the same collation. That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation.

Warning

Because indexes that are configured with collation use ICU collation keys to achieve sort order, collation-aware index keys may be larger than index keys for indexes without collation.

For example, the collection myColl has an index on a string field category with the collation locale "fr".

db.myColl.createIndex( { category: 1 }, { collation: { locale: "fr" } } )

The following query operation, which specifies the same collation as the index, can use the index:

db.myColl.find( { category: "cafe" } ).collation( { locale: "fr" } )

However, the following query operation, which by default uses the "simple" binary collator, cannot use the index:

db.myColl.find( { category: "cafe" } )

For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys.

For example, the collection myColl has a compound index on the numeric fields score and price and the string field category; the index is created with the collation locale "fr" for string comparisons:

db.myColl.createIndex(
{ score: 1, price: 1, category: 1 },
{ collation: { locale: "fr" } } )

The following operations, which use "simple" binary collation for string comparisons, can use the index:

db.myColl.find( { score: 5 } ).sort( { price: 1 } )
db.myColl.find( { score: 5, price: { $gt: NumberDecimal( "10" ) } } ).sort( { price: 1 } )

The following operation, which uses "simple" binary collation for string comparisons on the indexed category field, can use the index to fulfill only the score: 5 portion of the query:

db.myColl.find( { score: 5, category: "cafe" } )

Important

Matches against document keys, including embedded document keys, use simple binary comparison. This means that a query for a key like "foo.bár" will not match the key "foo.bar", regardless of the value you set for the strength parameter.

Back

Apply Patterns