Database Manual

Indexes索引

Indexes support efficient execution of queries in MongoDB. Without indexes, MongoDB must scan every document in a collection to return query results. If an appropriate index exists for a query, MongoDB uses the index to limit the number of documents it must scan.索引支持在MongoDB中高效执行查询。如果没有索引,MongoDB必须扫描集合中的每个文档以返回查询结果。如果查询存在适当的索引,MongoDB将使用该索引来限制必须扫描的文档数量。

Although indexes improve query performance, adding an index has negative performance impact for write operations. For collections with a high write-to-read ratio, indexes are expensive because each insert must also update any indexes.虽然索引可以提高查询性能,但添加索引会对写入操作的性能产生负面影响。对于具有高读写比的集合,索引很昂贵,因为每次插入都必须更新任何索引。

Use Cases用例

If your application is repeatedly running queries on the same fields, you can create an index on those fields to improve performance. For example, consider the following scenarios:如果应用程序在相同的字段上重复运行查询,您可以在这些字段上创建索引以提高性能。例如,考虑以下场景:

Scenario场景Index Type索引类型
A human resources department often needs to look up employees by employee ID. You can create an index on the employee ID field to improve query performance.人力资源部门通常需要按员工ID查找员工。您可以在员工ID字段上创建索引以提高查询性能。Single Field Index单字段索引

A salesperson often needs to look up client information by location. Location is stored in an embedded object with fields like state, city, and zipcode. 销售人员通常需要按位置查找客户信息。位置存储在一个嵌入式对象中,其中包含statecityzipcode(邮政编码)等字段。You can create an index on the location object to improve performance for queries on that object.您可以在location对象上创建索引,以提高对该对象的查询性能。

When you create an index on an embedded document, only queries that specify the entire embedded document use the index. Queries on a specific field within the document do not use the index.在嵌入式文档上创建索引时,只有指定整个嵌入式文档的查询才会使用该索引。对文档中特定字段的查询不使用索引。

Single Field Index on an embedded document嵌入式文档上的单字段索引
A grocery store manager often needs to look up inventory items by name and quantity to determine which items are low stock. 杂货店经理经常需要按名称和数量查找inventory物品,以确定哪些物品inventory不足。You can create a single index on both the item and quantity fields to improve query performance.您可以在itemquantity字段上创建单个索引,以提高查询性能。Compound Index复合指数

Get Started开始使用

You can create and manage indexes in MongoDB Atlas, with a driver method, or with the MongoDB Shell. MongoDB Atlas is the fully managed service for MongoDB deployments in the cloud.您可以使用驱动程序方法或MongoDB Shell在MongoDB Atlas中创建和管理索引。MongoDB Atlas是云中MongoDB部署的完全托管服务。

Create and Manage Indexes in MongoDB Atlas在MongoDB Atlas中创建和管理索引

For deployments hosted in MongoDB Atlas, you can create and manage indexes with the MongoDB Atlas UI or the Atlas CLI. MongoDB Atlas also includes a Performance Advisor that recommends indexes to improve slow queries, ranks suggested indexes by impact, and recommends which indexes to drop.对于托管在MongoDB Atlas中的部署,您可以使用MongoDB Atlas UI或Atlas CLI创建和管理索引。MongoDB Atlas还包括一个性能顾问,该顾问推荐索引以改善慢速查询,根据影响对建议的索引进行排名,并建议删除哪些索引。

To learn how to create and manage indexes the MongoDB Atlas UI or the Atlas CLI, see Create, View, Drop, and Hide Indexes.要了解如何在MongoDB Atlas UI或Atlas CLI中创建和管理索引,请参阅创建、查看、删除和隐藏索引

To learn more about the MongoDB Atlas Performance Advisor, see Monitor and Improve Slow Queries.要了解有关MongoDB Atlas性能顾问的更多信息,请参阅监控和改进慢速查询

Create and Manage Indexes with a Driver Method or the MongoDB Shell使用驱动程序方法或MongoDB Shell创建和管理索引

You can create and manage indexes with a driver method or the MongoDB Shell. To learn more, see the following resources:您可以使用驱动程序方法或MongoDB Shell创建和管理索引。要了解更多信息,请参阅以下资源:

Details详情

Indexes are special data structures that store a small portion of the collection's data set in an easy-to-traverse form. MongoDB indexes use a B-tree data structure.索引是一种特殊的数据结构,以易于遍历的形式存储集合数据集的一小部分。MongoDB索引使用B树数据结构。

The index stores the value of a specific field or set of fields, ordered by the value of the field. The ordering of the index entries supports efficient equality matches and range-based query operations. In addition, MongoDB can return sorted results using the ordering in the index.索引存储特定字段或字段集的值,按字段值排序。索引条目的排序支持高效的相等匹配和基于范围的查询操作。此外,MongoDB可以使用索引中的顺序返回排序结果。

Restrictions限制

Certain restrictions apply to indexes, such as the length of the index keys or the number of indexes per collection. For details, see Index Limitations.某些限制适用于索引,例如索引键的长度或每个集合的索引数。有关详细信息,请参阅索引限制

Default Index默认索引

MongoDB creates a unique index on the _id field during the creation of a collection. MongoDB在创建集合时在_id字段上创建一个唯一索引The _id index prevents clients from inserting two documents with the same value for the _id field. You cannot drop this index._id索引防止客户端插入具有相同_id字段值的两个文档。您不能删除此索引。

Note

In sharded clusters, if you do not use the _id field as the shard key, then your application must ensure the uniqueness of the values in the _id field. 分片集群中,如果你不使用_id字段作为分片键,那么应用程序必须确保_id字段中值的唯一性。You can do this by using a field with an auto-generated ObjectId.您可以通过使用具有自动生成的ObjectId的字段来实现这一点。

Index Names索引名称

The default name for an index is the concatenation of the indexed keys and each key's direction in the index (1 or -1) using underscores as a separator. For example, an index created on { item : 1, quantity: -1 } has the name item_1_quantity_-1.索引的默认名称是索引键和每个键在索引中的方向(1-1)的连接,使用下划线作为分隔符。例如,在{ item : 1, quantity: -1 }上创建的索引名为item_1_quantity_-1

You cannot rename an index once created. Instead, you must drop and recreate the index with a new name.索引一旦创建,就不能重命名。相反,您必须删除索引并用新名称重新创建。

To learn how to specify the name for an index, see Specify an Index Name.要了解如何指定索引的名称,请参阅指定索引名称

Index Build Performance指数构建性能

Applications may encounter reduced performance during index builds, including limited read/write access to the collection. 应用程序在索引构建过程中可能会遇到性能下降,包括对集合的读/写访问受限。For more information on the index build process, see Index Builds on Populated Collections, including the Index Builds in Replicated Environments section.有关索引构建过程的更多信息,请参阅填充集合上的索引构建,包括复制环境中的索引构建部分。

Learn More了解更多