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.本文档描述单个字段的升序/降序索引。

Diagram of an index on the ``score`` field (ascending).

Create an Ascending Index 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 } } )

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.嵌入字段上的索引与嵌入文档上的索引不同,后者包含索引中嵌入文档的最大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" } )

Create an Index on Embedded Document在嵌入文档上创建索引

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字段是一个嵌入式文档,包含citystate的嵌入式字段。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" } } )

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.有关查询嵌入文档的详细信息,请参见查询嵌入文档。

Additional Considerations其他注意事项

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.这对结果索引没有任何影响。