Created
July 7, 2020 12:06
-
-
Save davidebettio/59cab41d1719c36a13c5a85394d17a8d to your computer and use it in GitHub Desktop.
Backup mongodb indexes
This file contains hidden or 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
print(`// Backup indexes of : ${db.getName()} : database`); | |
print(`use ${db.getName()};`); | |
db.getCollectionNames().forEach(function (collection) { | |
indexes = db.getCollection(collection).getIndexes().forEach(function (index) { | |
if (index.name === '_id_') return; // skip defalut _id indexes | |
const keys = tojsononeline(index.key); | |
delete index.id; delete index.key; delete index.v; delete index.ns; | |
print(`db.${collection}.createIndex(${keys}, ${tojsononeline(index)});`); | |
}); | |
}); | |
mongo --quiet mongodb://localhost:27017/mydatabase indexes-backup.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment