Skip to content

Instantly share code, notes, and snippets.

@mhelmstetter
Created September 1, 2022 22:15
Show Gist options
  • Save mhelmstetter/70ef46c6bcad132e66d015c7ca22a5d1 to your computer and use it in GitHub Desktop.
Save mhelmstetter/70ef46c6bcad132e66d015c7ca22a5d1 to your computer and use it in GitHub Desktop.
db.adminCommand({'listDatabases':1, nameOnly:true}).databases.forEach(function(mydb) {
var dbName = mydb.name;
if (! ["admin", "config", "local"].includes(dbName)) {
var currentDB = db.getSiblingDB(dbName);
var collectionNames = currentDB.getCollectionNames();
collectionNames.forEach(function (collName) {
if (/^system\./.test(collName)){
return;
}
var indexes = currentDB[collName].getIndexes();
//print(` ${collName} ${indexes}`);
indexes.forEach(function(index){
delete index.v;
delete index.ns;
var key = index.key;
delete index.key
print(`db.getSiblingDB('${dbName}').${collName}.createIndex(${JSON.stringify(key)}, ${JSON.stringify(index)})`)
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment