у меня это работало в оболочке монго (v4.0.0):
db.getCollectionInfos().forEach(function(coll) {
if (coll.type === "collection" ) {
db[coll.name].getIndexes().forEach(function(index) {
if ("id" !== index.name) {
var indexKey = index.key
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) + ")");
}
});
}
});
и как один лайнер ...
db.getCollectionInfos().forEach(function(coll) { if (coll.type === "collection" ) { db[coll.name].getIndexes().forEach(function(index) { if ("id" !== index.name) { var indexKey = index.key delete index.v delete index.key index.background = true print("db." + coll.name + ".createIndex(" + JSON.stringify(indexKey) + ", " + JSON.stringify(index) + ")"); } }); } });