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
的集合,该集合包含类似以下示例文档的文档:
The following operation creates an ascending index on the 以下操作在score
field of the records
collection:records
集合的score
字段上创建升序索引:
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
上进行选择的查询,例如:
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.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
的集合,该集合包含类似以下示例文档的文档:
The following operation creates an index on the 下面的操作在location.state
field:location.state
字段:
The created index will support queries that select on the field 创建的索引将支持在字段上选择的查询location.state
, such as the following:location.state
,例如:
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
的集合,该集合包含类似以下示例文档的文档:
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
字段作为一个整体创建索引:
The following query can use the index on the 以下查询可以使用location
field:location
字段上的索引:
Note
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.NumberLong(1)
而不是1
作为规范。This does not have any affect on the resulting index.这对结果索引没有任何影响。