Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 17:23
Show Gist options
  • Save anonymous/4346929 to your computer and use it in GitHub Desktop.
Save anonymous/4346929 to your computer and use it in GitHub Desktop.
Redundant shard key indexes with mongo 2.2
db.getCollectionNames().forEach(function(n){
var shardConfig = db.getSisterDB('config').collections.findOne({_id:"foursquare." + n},{key:1});
if (shardConfig){
var shardKeyNameParts = [];
for (var keyPart in shardConfig.key) {
shardKeyNameParts.push(keyPart + "_" + shardConfig.key[keyPart]);
}
var shardKeyName = shardKeyNameParts.join('_');
var shardKeySizeMb = Math.round(db[n].stats().indexSizes[shardKeyName]/1024/1024);
db[n].getIndexes().forEach(function(i){
if (i.name.indexOf(shardKeyName) == 0 && i.name != shardKeyName) {
print("Possible duplicates for " + n + ": " + tojson(i.key) + " with shardKey " + tojson(shardConfig.key) + " will save " + shardKeySizeMb + " mb");
}
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment