Skip to content

Instantly share code, notes, and snippets.

@na1s
Created February 4, 2013 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save na1s/4705234 to your computer and use it in GitHub Desktop.
Save na1s/4705234 to your computer and use it in GitHub Desktop.
Mongodb index command generator from database
var r = new RegExp("system");
db.getCollectionNames().forEach(
function(c){
if(!r.test(c)){
var indexes = db[c].getIndexes();
indexes.forEach(function(i){
var val = i["key"].toSource();
var trimmedValue = val.substr(1,val.length-2);
if (trimmedValue !="{_id:1}")
{
print("db."+c+".ensureIndex("+trimmedValue+",{ \"name\":\"" + i["name"]+"\"});");
}
});}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment