Navigation
This version will reach end of life on Feb 2018. To upgrade, go to the Learn more about upgrading your version of MongoDB.

Rebuild Indexes

If you need to rebuild indexes for a collection you can use the db.collection.reIndex() method to rebuild all indexes on a collection in a single operation. This operation drops all indexes, including the _id index, and then rebuilds all indexes.

Process

The operation takes the following form:

db.accounts.reIndex()

MongoDB will return the following document when the operation completes:

{
        "nIndexesWas" : 2,
        "msg" : "indexes dropped for collection",
        "nIndexes" : 2,
        "indexes" : [
                {
                        "key" : {
                                "_id" : 1,
                                "tax-id" : 1
                        },
                        "ns" : "records.accounts",
                        "name" : "_id_"
                }
        ],
        "ok" : 1
}

This shell helper provides a wrapper around the reIndex database command. Your client library may have a different or additional interface for this operation.

Additional Considerations

Note

To build or rebuild indexes for a replica set see Build Indexes on Replica Sets.