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.
Create an Ascending Index on a Single Field
Consider a collection named records
that holds documents thatresemble 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 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:
- 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 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:
- {
- "_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 fieldlocation.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 thatresemble 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 fieldscity
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" } } )
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.