Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Created November 25, 2013 15:59
Show Gist options
  • Save chanmix51/7643632 to your computer and use it in GitHub Desktop.
Save chanmix51/7643632 to your computer and use it in GitHub Desktop.
how to remove a subset of a mongodb collection that only has certain fields.
db.collection.find({"changes.objectSid": { $exists: 1}, "changes.pwdLastSet": { $exists: 1 }}).forEach(function(doc) {
var other_found = false;
for (key in doc.changes) {
if (key != 'objectSid' && key != 'pwdLastSet') {
other_found = true;
break;
}
}
if (!other_found) {
printjson(doc);
db.collection.remove(doc);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment