Single Field Indexes单个字段索引
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.本文档描述了单个字段的升序/降序索引。
Create an Ascending Index on a Single Field在单个字段上创建升序索引
Consider a collection 考虑一个包含以下示例文档的schools
that contains the following sample document:schools
集合:
db.schools.insertOne(
{
"_id": ObjectId("570c04a4ad233577f97dc459"),
"studentsEnrolled": 1034,
"location": { state: "NY", city: "New York" }
}
)
The following operation creates an ascending index on the 以下操作在studentsEnrolled
field of the schools
collection:schools
集合的studentsEnrolled
字段上创建一个升序索引:
db.schools.createIndex( { studentsEnrolled: 1 } )
The value of the field in the index specification describes the kind of index for that field. 索引规范中字段的值描述了该字段的索引类型。For example, a value of 例如,值1指定按升序排列项目的索引。1
specifies an index that orders items in ascending order. A value of 值-1
specifies an index that orders items in descending order. -1
指定按降序排列项目的索引。For additional index types, see index types.有关其他索引类型,请参阅索引类型。
The created index supports queries that select on the field 创建的索引支持在字段studentsEnrolled
, such as the following:studentsEnrolled
中选择的查询,例如以下查询:
db.schools.find( { studentsEnrolled: 1034 } )
db.schools.find( { studentsEnrolled: { $gt: 500 } } )
Create an Index on an Embedded Field在嵌入字段上创建索引
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"),
"studentsEnrolled": 1034,
"location": { state: "NY", city: "New York" }
}
The following operation creates an index on the 以下操作在location.state
field:location.state
字段上创建索引:
db.schools.createIndex( { "location.state": 1 } )
The created index will support queries that select on the field 创建的索引将支持在字段location.state
, such as the following:location.state
上进行选择的查询,例如以下查询:
db.schools.find( { "location.state": "CA" } )
db.schools.find( { "location.city": "Albany", "location.state": "NY" } )
Create an Index on Embedded Document在嵌入文档上创建索引
You can also create indexes on embedded document as a whole.您还可以将嵌入文档作为一个整体创建索引。
Consider a collection named 考虑一个名为schools
that holds documents that resemble the following sample document:schools
的集合,其中包含与以下示例文档相似的文档:
{
"_id": ObjectId("570c04a4ad233577f97dc459"),
"studentsEnrolled": 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.schools.createIndex( { location: 1 } )
The following query can use the index on the 以下查询可以使用location
field:location
字段上的索引:
db.schools.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.有关查询嵌入文档的详细信息,请参阅查询嵌入文档。
Additional Considerations其他注意事项
During index builds, applications may encounter reduced performance or limited read/write access to the collection being indexed.在索引构建过程中,应用程序可能会遇到性能下降或对正在索引的集合的读/写访问受限的情况。
For more information on the index build process, see Index Builds on Populated Collections, especially the Index Builds in Replicated Environments section.有关索引生成过程的详细信息,请参阅填充集合上的索引生成,特别是复制环境中的索引生成部分。
Some drivers use 一些驱动程序使用NumberLong(1)
instead of 1
to specify the index order. NumberLong(1)
而不是1
来指定索引顺序。The resulting indexes are the same.生成的索引是相同的。