Single Field Indexes

MongoDB provides complete support for indexes on any field in acollection of documents. By default, allcollections have an index on the _id field, andapplications and users may add additional indexes to support importantqueries 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 thatresemble the following sample document:

  1. {
  2. "_id": ObjectId("570c04a4ad233577f97dc459"),
  3. "score": 1034,
  4. "location": { state: "NY", city: "New York" }
  5. }

The following operation creates an ascending index on the scorefield of the records collection:

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

The value of the field in the index specification describes the kind ofindex for that field. For example, a value of 1 specifies an indexthat orders items in ascending order. A value of -1 specifies anindex that orders items in descending order. For additional indextypes, see index types.

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

  1. db.records.find( { score: 2 } )
  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 youcan index top-level fields in documents. Indexes on embedded fieldsdiffer 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 onembedded fields allow you to use a “dot notation,” to introspect intoembedded documents.

Consider a collection named records that holds documents thatresemble the following sample document:

  1. {
  2. "_id": ObjectId("570c04a4ad233577f97dc459"),
  3. "score": 1034,
  4. "location": { state: "NY", city: "New York" }
  5. }

The following operation creates an index on the location.statefield:

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

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

  1. db.records.find( { "location.state": "CA" } )
  2. 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 thatresemble the following sample document:

  1. {
  2. "_id": ObjectId("570c04a4ad233577f97dc459"),
  3. "score": 1034,
  4. "location": { state: "NY", city: "New York" }
  5. }

The location field is an embedded document, containing the embedded fieldscity and state. The following command creates an index on the locationfield as a whole:

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

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

  1. db.records.find( { location: { city: "New York", state: "NY" } } )

Note

Although the query can use the index, the result set does notinclude the sample document above. When performing equality matches onembedded documents, field order matters and the embedded documentsmust match exactly. See Query Embedded Documents for moreinformation regarding querying on embedded documents.

Additional Considerations

Applications may encounter reduced performance during indexbuilds, including limited read/write access to the collection. Formore information on the index build process, seeIndex Builds on Populated Collections, including theIndex Builds in Replicated Environments section.

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