Skip to content

Instantly share code, notes, and snippets.

@elfeffe
Forked from wpottier/show-indexes.js
Created February 10, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfeffe/ac252e443eb56f701f5383560532e9ea to your computer and use it in GitHub Desktop.
Save elfeffe/ac252e443eb56f701f5383560532e9ea to your computer and use it in GitHub Desktop.
MongoDB copy indexes
db.getCollectionNames().forEach(function(collection) {
indexes = db[collection].getIndexes();
indexes.forEach(function (c) {
opt = '';
ixkey = JSON.stringify(c.key, null, 1).replace(/(\r\n|\n|\r)/gm,"");
ns = c.ns.substr(c.ns.indexOf(".") + 1, c.ns.length);
for (var key in c) {
if (key != 'key' && key != 'ns' && key != 'v') {
if (opt != '') { opt+= ','}
if (c.hasOwnProperty(key)) {
if (typeof(c[key]) == "string") {
opt += (key + ': "' + c[key] + '"');
} else {
opt+= (key + ": " + c[key]);
}
}
}
}
if (opt != '') { opt = '{' + opt + '}'}
print ('db.' + ns + '.ensureIndex(' + ixkey + ','+ opt + ')');
})});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment