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. 嵌入字段上的索引与嵌入文档上的索引不同,后者包含索引中嵌入文档最大索引大小之前的全部内容。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字段是一个嵌入文档,包含嵌入字段citystateThe 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. This does not have any affect on the resulting index.一些驱动程序可以使用NumberLong(1)而不是1作为规范来指定索引。这对结果索引没有任何影响。

←  IndexesCompound Indexes →