Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mvasilenko/424cb2e334170d7992946e9f1cf93eed to your computer and use it in GitHub Desktop.
Save mvasilenko/424cb2e334170d7992946e9f1cf93eed to your computer and use it in GitHub Desktop.
MongoDB copy all indexes for collection
db.getCollectionInfos().forEach(function(coll) {
if (coll.type === "collection" ) {
db[coll.name].getIndexes().forEach(function(index) {
if ("id" !== index.name) {
//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." + coll.name + ".createIndex(" + JSON.stringify(indexKey) + ", " + JSON.stringify(index) + ")");
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment