Skip to content

Instantly share code, notes, and snippets.

@aktasch
Forked from mhelmstetter/export_indexes.js
Created October 25, 2022 03:50
Show Gist options
  • Save aktasch/481e73eee901a4fae6c8da5ce1b4aeab to your computer and use it in GitHub Desktop.
Save aktasch/481e73eee901a4fae6c8da5ce1b4aeab 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