Created
March 6, 2019 07:30
-
-
Save mvasilenko/424cb2e334170d7992946e9f1cf93eed to your computer and use it in GitHub Desktop.
MongoDB copy all indexes for collection
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
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