Create an Index
On this page
Indexes support efficient execution of queries in MongoDB. If your application is repeatedly running queries on the same fields, you can create an index on those fields to improve performance for those queries.
To create an index, use the createIndex()
shell
method or equivalent method for your driver. This page shows examples
for the MongoDB Shell and drivers.
About this Task
When you run a create index command in the MongoDB Shell or a driver, MongoDB only creates the index if an index of the same specification does not exist.
Although indexes improve query performance, adding an index has negative performance impact for write operations. For collections with a high write-to-read ratio, indexes are expensive because each insert and update must also update any indexes.
Procedure
➤ To set the language of the examples on this page, use the Select your language drop-down menu in the right navigation pane.
To create an index in mongosh
, use
db.collection.createIndex()
.
db.collection.createIndex( <key and index type specification>, <options> )
To create an index using the .NET driver, use MongoCollection.CreateIndex.
collection.CreateIndex( IndexKeys<collection>.<key and index type specification>, <options> );
To create an index using the Async Java driver, use com.mongodb.async.client.MongoCollection.createIndex.
collection.createIndex( <key and index type specification>, <options>, <callbackFunction>)
To create an index using the Java driver, use com.mongodb.client.MongoCollection.createIndex.
collection.createIndex( <key and index type specification>, <options> )å
To create an index using the
Motor driver,
use
motor.motor_asyncio.AsyncIOMotorCollection.create_index
.
await db.collection.create_index([(<key and index type specification>)], <options> )
To create an index using the
Node.JS driver,
use createIndex()
.
collection.createIndex( { <key and index type specification> }, function(err, result) { console.log(result); callback(result); }
To create an index using the Perl driver, use create_one().
my $indexes = $db->get_collection( <collection> )->indexes; $indexes->create_one( [ <key and index type specification> ] );
To create an index using the
PHP driver, use
MongoDB\\Collection::createIndex()
.
$collection->createIndex(<key and index type specification>, <options>);
To create an index using the
Python driver,
use pymongo.collection.Collection.create_index
:
db.collection.create_index([(<key and index type specification>)], <options> )
To create an index using the Ruby driver, use Mongo::Index::View#create_one.
client[:collection].indexes.create_one({ <key and index type specification> }, {options})
To create an index using the Scala driver, use org.mongodb.scala.model.Indexes.
collection.createIndex(<key and index type specification>)
Example
This example creates a single key descending index on the
name
field:
db.collection.createIndex( { name: -1 } )
This example creates a single key descending index on the
name
field:
collection.CreateIndex( IndexKeys<collection>.Descending("name") );
This example creates a single key descending index on the
name
field:
collection.createIndex(Indexes.descending("name"), someCallbackFunction());
This example creates a single key descending index on the
name
field:
collection.createIndex(Indexes.descending("name"));
This example creates a single key descending index on the
name
field:
await collection.create_index([("name", pymongo.DESCENDING)])
This example creates a single key descending index on the
name
field:
collection.createIndex( { name : -1 }, function(err, result) { console.log(result); callback(result); }
This example creates a single key descending index on the
name
field:
my $indexes = $db->get_collection( <collection> )->indexes; $indexes->create_one( [ name => -1 ] );
This example creates a single key descending index on the
name
field:
$collection->createIndex(['name' => -1]);
This example creates a single key descending index on the
name
field:
collection.create_index([("name", pymongo.DESCENDING)])
This example creates a single key descending index on the
name
field:
client[:collection].indexes.create_one({ name: -1 })
This example creates a single key descending index on the
name
field:
collection.createIndex(descending("name"))
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.
Results
To confirm that the index was created, use mongosh
to run the
db.collection.getIndexes()
method:
db.collection.getIndexes()
Output:
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { name: -1 }, name: 'name_-1' } ]
To view information on created indexes using a driver, refer to your driver's documentation.
Learn More
To learn how to create indexes in MongoDB Compass, see Manage Indexes in the Compass documentation.
To see how often your indexes are used, see Measure Index Use.
To learn how to specify the name of your index, see Specify an Index Name.
To learn how MongoDB builds indexes, see Index Build Process.