Docs HomeMongoDB Manual

Create Indexes to Support Your Queries创建索引以支持查询

An index supports a query when the index contains all the fields scanned by the query. 当索引包含查询扫描的所有字段时,索引支持查询。The query scans the index and not the collection. 查询扫描的是索引,而不是集合。Creating indexes that support queries results in greatly increased query performance.创建支持查询的索引可以大大提高查询性能。

This document describes strategies for creating indexes that support queries.本文档描述了创建支持查询的索引的策略。

Create a Single-Key Index if All Queries Use the Same, Single Key如果所有查询都使用相同的单键,则创建单键索引

If you only ever query on a single key in a given collection, then you need to create just one single-key index for that collection. 如果只查询给定集合中的单个键,那么只需要为该集合创建一个单个键索引。For example, you might create an index on category in the product collection:例如,您可以在product集合中的category上创建索引:

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

Create Compound Indexes to Support Several Different Queries创建复合索引以支持多种不同的查询

If you sometimes query on only one key and at other times query on that key combined with a second key, then creating a compound index is more efficient than creating a single-key index. 如果有时只查询一个键,而有时则查询该键与第二个键的组合,则创建复合索引比创建单个键索引更有效。MongoDB will use the compound index for both queries. MongoDB将对这两个查询使用复合索引。For example, you might create an index on both category and item.例如,您可以在categoryitem上创建索引。

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

This allows you both options. You can query on just category, and you also can query on category combined with item. 这两种选择都可以。您可以只查询category,也可以categoryitem组合的类别。A single compound index on multiple fields can support all the queries that search a "prefix" subset of those fields.多个字段上的单个复合索引可以支持搜索这些字段的“前缀”子集的所有查询。

Example

The following index on a collection:集合上的以下索引:

{ x: 1, y: 1, z: 1 }

Can support queries that the following indexes support:可以支持以下索引支持的查询:

{ x: 1 }
{ x: 1, y: 1 }

There are some situations where the prefix indexes may offer better query performance: for example if z is a large array.在某些情况下,前缀索引可以提供更好的查询性能:例如,如果z是一个大数组。

The { x: 1, y: 1, z: 1 } index can also support many of the same queries as the following index:{ x: 1, y: 1, z: 1 }索引还可以支持与以下索引相同的许多查询:

{ x: 1, z: 1 }

Also, { x: 1, z: 1 } has an additional use. Given the following query:此外,{ x: 1, z: 1 }还有其他用途。给定以下查询:

db.collection.find( { x: 5 } ).sort( { z: 1} )

The { x: 1, z: 1 } index supports both the query and the sort operation, while the { x: 1, y: 1, z: 1 } index only supports the query. { x: 1, z: 1 }索引同时支持查询和排序操作,而{ x: 1, y: 1, z: 1 }索引仅支持查询。For more information on sorting, see Use Indexes to Sort Query Results.有关排序的详细信息,请参阅使用索引对查询结果进行排序

Create Indexes to Support Text Search创建索引以支持文本搜索

For data hosted on MongoDB Atlas, you can support full-text search with Atlas Search indexes. 对于MongoDB Atlas上托管的数据,您可以使用Atlas search索引支持全文搜索。To learn more, see Create an Atlas Search Index.要了解更多信息,请参阅创建Atlas搜索索引

For self-managed (non-Atlas) deployments, MongoDB provides a text index type that supports searching for string content in a collection. 对于自管理(非Atlas)部署,MongoDB提供了一种text索引类型,支持搜索集合中的字符串内容。To learn more about self-managed text indexes, see Text Indexes.要了解有关自管理文本索引的更多信息,请参阅文本索引

Index Use and Collation索引使用和排序

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.也就是说,如果具有排序规则的索引指定了不同的排序规则,则该索引无法支持对索引字段执行字符串比较的操作。

For example, the collection myColl has an index on a string field category with the collation locale "fr".例如,集合myColl在排序规则区域设置为"fr"的字符串字段category上有一个索引。

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:例如,集合myColl对数字字段scoreprice以及字符串字段category有一个复合索引;索引是使用排序规则区域设置"fr"创建的,用于字符串比较:

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:以下操作使用"simple"二进制排序规则进行字符串比较,可以使用索引:

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:以下操作使用"simple"二进制排序规则对索引类别字段进行字符串比较,可以使用索引仅完成查询的score: 5部分:

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