Navigation
This version of the documentation is archived and no longer supported.

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 indexes for a replica set see Build Indexes on Replica Sets.