Docs HomeMongoDB Manual

Indexes索引

Indexes support efficient execution of queries in MongoDB. 索引支持在MongoDB中高效执行查询。Without indexes, MongoDB must scan every document in a collection to return query results. 在没有索引的情况下,MongoDB必须扫描集合中的每个文档才能返回查询结果。If an appropriate index exists for a query, MongoDB uses the index to limit the number of documents it must scan.如果查询存在适当的索引,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. 人力资源部门通常需要根据员工ID查找员工。You can create an index on the employee ID field to improve query performance.您可以在“员工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 entire location object to improve performance for queries on any field in that object.可以在整个location对象上创建索引,以提高该对象中任何字段的查询性能。Single Field Index on an object对象上的单个字段索引
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.您可以在item字段和quantity字段上创建一个索引,以提高查询性能。Compound Index复合指数

Get Started开始

To learn how to perform common tasks with indexes, see these pages:要了解如何使用索引执行常见任务,请参阅以下页面:

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. _id索引防止客户端插入两个具有相同_id字段值的文档。You cannot drop this index.不能删除此索引。

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. 索引的默认名称是索引键和索引中每个键的方向(1-1)的串联,使用下划线作为分隔符。For example, an index created on { item : 1, quantity: -1 } has the name item_1_quantity_-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了解更多信息

  • MongoDB provides a number of different index types to support specific types of data and queries. MongoDB提供了许多不同的索引类型来支持特定类型的数据和查询。To learn more, see Index Types.要了解更多信息,请参阅索引类型
  • To learn what properties and behaviors you can specify in your index, see Index Properties.要了解可以在索引中指定哪些属性和行为,请参阅索引属性
  • To understand considerations you may need to make when you create an index, see Indexing Strategies.要了解创建索引时可能需要考虑的事项,请参阅索引策略
  • To learn about the performance impact of indexes, see Operational Factors and Data Models.要了解索引对性能的影响,请参阅操作因素和数据模型