MongoDB Script Get all Indexes Creation Script
MongoDB: Get All Indexes in DB via Mongo Shell
This is a simple script to backup all the index creation scripts
Note: background:true is deprecated since MongoDB 4.2, but it's okay to leave it in.
Options for all Index types
To run this, simply save to a script and run it with mongo shell
mongo -u admin -p --authenticationDatabase=admin --host localhost getAllIndexesCreationScript.js > output.js
db.getMongo().getDBNames().forEach(function(dbName) {
if (dbName != "admin" && dbName != "local") {
db.getSiblingDB(dbName).getCollectionNames().forEach(function(coll) {
db.getSiblingDB(dbName)[coll].getIndexes().forEach(function(index) {
if ("_id_" !== index.name) {
print("db.getSiblingDB('" + dbName + "')." + coll + ".createIndex(" + tojson(index.key) + ",{background:true}" + ")");
}
});
});
}
});
Comments
Post a Comment