Team to reindex all mongodb collections

Links to MongoDB re-indexing collections are usually stored in the collection:

db.mycollection.reIndex();

I would like to re-index several collections at once. One after the other can get a little tired.

Which corresponding team issues reIndex();for all collections?

+5
source share
2 answers

How about this? It is still one after another for the database, but only one command for you.

db.getCollectionNames().forEach(function(coll_name) {
  var coll = db.getCollection(coll_name);
  coll.reIndex();
});
+13
source

A slightly smaller version of Sergio's answer:

db.getCollectionNames().forEach(function(collection){db[collection].reIndex()});

There is no need to get a collection link first.

+15
source

All Articles