I needed a reliable way to take the indexes from a staging instance and ensure they're replicated to production.
The following _MONGOSH code loops over every collection and then every index in the collection, creating a "createIndex" command for each combination. This makes it trival to copy indexes from one environment to the other.
db.getCollectionNames().forEach(function(col) {var indexes = db[col].getIndexes();indexes.forEach(function (c) {var fields = '', result = '', options = {};for (var i in c) {if (i == 'key') {fields = c[i];} else if (i == 'name' && c[i] == '_id_') {return;} else if (i != 'name' && i != 'v' && i != 'ns') {options[i] = c[i];}}var fields = JSON.stringify(fields);var options = JSON.stringify(options);if (options == '{}') {result = "db." + col + ".createIndex(" + fields + "); ";} else {result = "db." + col + ".createIndex(" + fields + ", " + options + "); ";}result = result.replace(/{"floatApprox":-1,"top":-1,"bottom":-1}/ig, '-1').replace(/{"floatApprox":(-?\d+)}/ig, '$1').replace(/\{"\$numberLong":"(-?\d+)"\}/ig, '$1');print(result);});});
Credit:
- Original / archived by Aleksandr Maiorov
- Stack Overflow