翻译或纠错本页面

单键索引

MongoDB provides complete support for indexes on any field in a collection of documents. 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.

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:

{
  "_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:

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. A value of -1 specifies an index that orders items in descending order. For additional index types, see index types.

The created index will support queries that select on the field score, such as the following:

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:

{
  "_id": ObjectId("570c04a4ad233577f97dc459"),
  "score": 1034,
  "location": { state: "NY", city: "New York" }
}

The following operation creates an index on the location.state field:

db.records.createIndex( { "location.state": 1 } )

The created index will support queries that select on the field location.state, such as the following:

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:

{
  "_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. The following command creates an index on the location field as a whole:

db.records.createIndex( { location: 1 } )

The following query can use the index on the location field:

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.

Additional Considerations

If your collection holds a large amount of data, and your application needs to be able to access the data while building the index, consider building the index in the background, as described in 后台创建.

To build or rebuild indexes for a replica set, see 在复制集上创建索引.

Some drivers may specify indexes, using NumberLong(1) rather than 1 as the specification. This does not have any affect on the resulting index.

←   Indexes 复合索引  →