Mongodb database index ddl

      No Comments on Mongodb database index ddl

You can use below script to check how many existing indexes you have in your Mongo database for all collections. this is really helpful when you have one environment working fine and same database in other environment is performance is bad , you can take existing Index ddl and create update Indexes as per your requirement to have better performance.

db.getCollectionInfos().forEach(function(coll) {
//print( JSON.stringify( coll ))
if (coll.type === “collection” ) {
db[].getIndexes().forEach(function(index) {
if (“id” !== {
//print( JSON.stringify( index ))
var indexKey = index.key // save the key, and transform index into the “options”
delete index.v
delete index.key
index.background = true // optional: force background to be true
print(“db.” + + “.createIndex(” + JSON.stringify(indexKey) + “, ” + JSON.stringify(index) + “)”);