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:如果应用程序在相同的字段上重复运行查询,您可以在这些字段上创建索引以提高性能。例如,考虑以下场景:
| |
inventory物品,以确定哪些物品inventory不足。item and quantity fields to improve query performance.item和quantity字段上创建单个索引,以提高查询性能。 |
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
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了解更多
MongoDB provides a number of different index types to support specific types of data and queries. To learn more, see Index Types.MongoDB提供了许多不同的索引类型来支持特定类型的数据和查询。要了解更多信息,请参阅索引类型。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.要了解指标的性能影响,请参阅操作因素和数据模型。To learn about query settings and indexes, see要了解查询设置和索引,请参阅setQuerySettings.setQuerySettings。