Indexes support efficient execution of queries in MongoDB. Withoutindexes, MongoDB must scan every document in a collection to returnquery results. If an appropriate index exists for a query, MongoDB usesthe index to limit the number of documents it must scan.
Although indexes improve query performance, adding an index has negativeperformance impact for write operations. For collections with a highwrite-to-read ratio, indexes are expensive because each insert must alsoupdate any indexes.
Use Cases
If your application is repeatedly running queries on the same fields,you can create an index on those fields to improve performance. Forexample, consider the following scenarios:
Scenario | Index Type |
---|---|
A human resources department often needs to look up employees byemployee ID. You can create an index on the employee ID field toimprove query performance. | |
A salesperson often needs to look up client information bylocation. Location is stored in an embedded object with fieldslike When you create an index on an embedded document, only queries thatspecify the entire embedded document use the index. Queries on aspecific field within the document do not use the index. | Single Field Index on an embeddeddocument |
A grocery store manager often needs to look up inventory items byname and quantity to determine which items are low stock. You cancreate a single index on both the |
Get Started
You can create and manage indexes inMongoDB Atlas, with a drivermethod, or with the MongoDB Shell. MongoDB Atlas is the fullymanaged service for MongoDB deployments in the cloud.
Create and Manage Indexes in MongoDB Atlas
For deployments hosted in MongoDB Atlas, you can createand manage indexes with the MongoDB Atlas UI or the Atlas CLI. MongoDB Atlasalso includes a Performance Advisor that recommends indexes to improveslow queries, ranks suggested indexes by impact, and recommends whichindexes to drop.
To learn how to create and manage indexes the MongoDB Atlas UI or the AtlasCLI, seeCreate, View, Drop, and Hide Indexes.
To learn more about the MongoDB Atlas Performance Advisor, seeMonitor and Improve Slow Queries.
Create and Manage Indexes with a Driver Method or the MongoDB Shell
You can create and manage indexes with a driver method or the MongoDBShell. To learn more, see the following resources:
Details
Indexes are special data structures that store a small portion of thecollection's data set in an easy-to-traverse form. MongoDB indexes use aB-tree data structure.
The index stores the value of a specific field or set of fields, orderedby the value of the field. The ordering of the index entries supportsefficient equality matches and range-based query operations. Inaddition, MongoDB can return sorted results using the ordering in theindex.
Restrictions
Certain restrictions apply to indexes, such as the length of the indexkeys or the number of indexes per collection. For details, seeIndex Limitations.
Default Index
MongoDB creates aunique index on the_id field during the creation of acollection. The_id
index prevents clients from inserting twodocuments with the same value for the_id
field. You cannot dropthis index.
Note
Insharded clusters, if you donot usethe_id
field as theshard key, then your applicationmust ensure the uniqueness of the values in the_id
field.You can do this by using a field with an auto-generatedObjectId.
Index Names
The default name for an index is the concatenation of the indexed keysand each key's direction in the index (1
or-1
) using underscoresas a separator. For example, an index created on{ item : 1, quantity:-1 }
has the nameitem_1_quantity_-1
.
You cannot rename an index once created. Instead, you mustdrop and recreate the index with a new name.
To learn how to specify the name for an index, seeSpecify an Index Name.
Index Build Performance
Applications may encounter reduced performance during index builds,including limited read/write access to the collection. For moreinformation on the index build process, seeIndex Builds on Populated Collections,including theIndex Builds in Replicated Environments section.
Learn More
MongoDB provides a number of different index types to support specifictypes of data and queries. To learn more, seeIndex Types.
To learn what properties and behaviors you can specify in your index,seeIndex Properties.
To understand considerations you may need to make when you create anindex, seeIndexing Strategies.
To learn about the performance impact of indexes, seeOperational Factors and Data Models.
To learn about query settings and indexes, see
setQuerySettings
.