Create an Index on a Single Field
On this page
You can create an index on a single field to improve performance for queries on that field. Indexing commonly queried fields increases the chances of covering those queries, meaning MongoDB can satisfy the query entirely with the index, without examining documents.
To create a single-field index, use the
db.collection.createIndex()
method:
db.<collection>.createIndex( { <field>: <sortOrder> } )
Note
Index Sort Order
For a single-field index, the sort order (ascending or descending) of the index key does not matter because MongoDB can traverse the index in either direction.
Before You Begin
Create a students
collection that contains the following documents:
db.students.insertMany( [ { "name": "Alice", "gpa": 3.6, "location": { city: "Sacramento", state: "California" } }, { "name": "Bob", "gpa": 3.2, "location": { city: "Albany", state: "New York" } } ] )
Procedures
The following examples show you how to:
Create an Index on a Single Field
Consider a school administrator who frequently looks up students by
their GPA. You can create an index on the
gpa
field to improve performance for those queries:
db.students.createIndex( { gpa: 1 } )
Results
The index supports queries that select on the field gpa
, such as the
following:
db.students.find( { gpa: 3.6 } ) db.students.find( { gpa: { $lt: 3.4 } } )
Create an Index on an Embedded Document
You can create indexes on embedded documents as a whole.
Consider a social networking application where students can search for
one another by location. Student location is stored in an embedded
document called location
. The location
document contains the
fields city
and state
.
You can create an index on the location
field to improve performance
for queries on the location
document:
db.students.createIndex( { location: 1 } )
Results
The following query uses the index on the location
field:
db.students.find( { location: { city: "Sacramento", state: "California" } } )
Important
Field Order for Embedded Documents
When you query based on embedded documents, the order that specify fields matters. The embedded documents in your query and returned document must match exactly. To see more examples of queries on embedded documents, see Query on Embedded/Nested Documents.
Details
When you create an index on an embedded document, only queries that specify the entire embedded document use the index. Queries on a specific field within the document do not use the index.
For example, the following queries do not use the index on the
location
field because they query on specific fields within the
embedded document:
db.students.find( { "location.city": "Sacramento" } ) db.students.find( { "location.state": "New York" } )
In order for a dot notation query to use an index, you must create an index on the specific embedded field you are querying, not the entire embedded object. For an example, see Create an Index on an Embedded Field.
Create an Index on an Embedded Field
You can create indexes on fields within embedded documents. Indexes on embedded fields can fulfill queries that use dot notation.
The location
field is an embedded document that contains the
embedded fields city
and state
. Create an index on the
location.state
field:
db.students.createIndex( { "location.state": 1 } )
Results
The index supports queries on the field location.state
, such as the
following:
db.students.find( { "location.state": "California" } ) db.students.find( { "location.city": "Albany", "location.state": "New York" } )