References to reindexing MongoDB collections are usually per collection:
db.mycollection.reIndex();
I'd like to reindex a number of collections all at once. One-by-one can get a bit tiring.
What's the appropriate command to issue reIndex();
across all collections?
To list all collections in Mongo shell, you can use the function getCollectionNames().
According to MongoDB documentation: Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.
MongoDB provides two geospatial indexes known as 2d indexes and 2d sphere indexes using these indexes we can query geospatial data.
Slightly smaller version of Sergio's answer:
db.getCollectionNames().forEach(function(collection){db[collection].reIndex()});
There is no need to get a reference to the collection first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With