Created
February 22, 2016 15:39
-
-
Save AvnerCohen/0405509b7ab873c83ce3 to your computer and use it in GitHub Desktop.
A script to print out all the indexes in all database of mongo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var db = db.getSiblingDB("admin"); | |
var dbs = db.runCommand({ "listDatabases": 1 }).databases.sort(); | |
dbs.forEach(function(database) { | |
db = db.getSiblingDB(database.name); | |
cols = db.getCollectionNames().sort(); | |
cols.forEach(function(col) { | |
if (!db[col]) { return; } | |
db[col].getIndexes().sort().forEach(function(index) { | |
if ("_id_" !== index.name) { | |
print("at database: [" + database.name +"], db." + col + ".ensureIndex(" + tojson(index.key) + ")"); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment