On this page本页内容
MongoDB provides complete support for indexes on any field in a collection of documents. MongoDB完全支持文档集合中任何字段的索引。By default, all collections have an index on the _id field, and applications and users may add additional indexes to support important queries and operations.默认情况下,所有集合在_id
字段上都有一个索引,应用程序和用户可以添加其他索引以支持重要的查询和操作。
This document describes ascending/descending indexes on a single field.本文档描述了单个字段的升序/降序索引。
Consider a collection named 考虑一个名为records
that holds documents that resemble the following sample document:records
的集合,该集合包含与以下示例文档相似的文档:
{ "_id": ObjectId("570c04a4ad233577f97dc459"), "score": 1034, "location": { state: "NY", city: "New York" } }
The following operation creates an ascending index on the 以下操作在score
field of the records
collection:records
集合的score
字段上创建升序索引:
db.records.createIndex( { score: 1 } )
The value of the field in the index specification describes the kind of index for that field. 索引规范中字段的值描述了该字段的索引类型。For example, a value of 例如,值为1
specifies an index that orders items in ascending order. 1
指定按升序排序项目的索引。A value of 值-1
specifies an index that orders items in descending order. -1
指定按降序排列项目的索引。For additional index types, see index types.有关其他索引类型,请参阅索引类型。
The created index will support queries that select on the field 创建的索引将支持对字段score
, such as the following:score
进行选择的查询,例如:
db.records.find( { score: 2 } ) db.records.find( { score: { $gt: 10 } } )
You can create indexes on fields within embedded documents, just as you can index top-level fields in documents. 您可以对嵌入文档中的字段创建索引,就像您可以对文档中的顶级字段进行索引一样。Indexes on embedded fields differ from indexes on embedded documents, which include the full content up to the maximum index size of the embedded document in the index. 嵌入字段上的索引与嵌入文档上的索引不同,后者包含索引中嵌入文档最大索引大小之前的全部内容。Instead, indexes on embedded fields allow you to use a "dot notation," to introspect into embedded documents.相反,嵌入字段上的索引允许您使用“点符号”来内省嵌入文档。
Consider a collection named 考虑一个名为records
that holds documents that resemble the following sample document:records
的集合,该集合包含与以下示例文档相似的文档:
{ "_id": ObjectId("570c04a4ad233577f97dc459"), "score": 1034, "location": { state: "NY", city: "New York" } }
The following operation creates an index on the 以下操作在location.state
field:location.state
字段上创建索引:
db.records.createIndex( { "location.state": 1 } )
The created index will support queries that select on the field 创建的索引将支持对字段location.state
, such as the following:location.state
进行选择的查询,例如:
db.records.find( { "location.state": "CA" } ) db.records.find( { "location.city": "Albany", "location.state": "NY" } )
You can also create indexes on embedded document as a whole.您还可以将嵌入文档作为一个整体创建索引。
Consider a collection named 考虑一个名为records
that holds documents that resemble the following sample document:records
的集合,该集合包含与以下示例文档相似的文档:
{ "_id": ObjectId("570c04a4ad233577f97dc459"), "score": 1034, "location": { state: "NY", city: "New York" } }
The location
field is an embedded document, containing the embedded fields city
and state
. location
字段是一个嵌入文档,包含嵌入字段city
和state
。The following command creates an index on the 以下命令将在整个location
field as a whole:location
字段上创建索引:
db.records.createIndex( { location: 1 } )
The following query can use the index on the 以下查询可以使用location
field:location
字段上的索引:
db.records.find( { location: { city: "New York", state: "NY" } } )
Although the query can use the index, the result set does not include the sample document above. 虽然查询可以使用索引,但结果集不包括上面的示例文档。When performing equality matches on embedded documents, field order matters and the embedded documents must match exactly. 对嵌入文档执行相等匹配时,字段顺序很重要,嵌入文档必须精确匹配。See Query Embedded Documents for more information regarding querying on embedded documents.有关查询嵌入文档的更多信息,请参阅查询嵌入文档。
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.有关索引生成过程的更多信息,请参阅填充集合上的索引生成,包括复制环境中的索引生成部分。
Some drivers may specify indexes, using 一些驱动程序可以使用NumberLong(1)
rather than 1
as the specification. This does not have any affect on the resulting index.NumberLong(1)
而不是1
作为规范来指定索引。这对结果索引没有任何影响。