Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Created February 22, 2016 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AvnerCohen/0405509b7ab873c83ce3 to your computer and use it in GitHub Desktop.
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
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