skip to Main Content

In mongo, you can rebuild all indexes with

db.collection.reIndex()

But is there a way to rebuild a single index?

Thanks,
Kevin

2

Answers


  1. The db.collection.reIndex() drops all indexes on a collection and recreates them. This operation may be expensive for collections that have a large amount of data and/or a large number of indexes.

    It’s very costly operation and blocks all the operations on the collection for the duration.

    Use dropIndex to remove one Index and createIndex to create it back as rebuildIndex is not supported for only one index

    And 5.0 onwards, reIndex will be restricted to standalone instances.

    Login or Signup to reply.
  2. Identify all your indexes with:

    db.collection.getIndexes();
    

    Find the one you want to rebuild, e.g.:

        {
            "v" : 2,
            "key" : {
                "fld1" : 1,
                "fld2" : -1
            },
            "name" : "fld1_1_fld2_-1"
        }
    

    "Safely" drop and rebuild with explicit specification instead of name:

    var idx={fld1:1,fld2:-1};db.collection.dropIndex(idx);db.collection.createIndex(idx);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search