Skip to content

Instantly share code, notes, and snippets.

@usametov
Last active April 1, 2018 01:07
Show Gist options
  • Save usametov/ed6d59da531fad0790b62f69b0c85f87 to your computer and use it in GitHub Desktop.
Save usametov/ed6d59da531fad0790b62f69b0c85f87 to your computer and use it in GitHub Desktop.
mongodb snippet: search for a value in all fields
db.somethings.find({$where: function() {
var deepIterate = function (obj, value) {
for (var field in obj) {
if (obj[field] == value){
return true;
}
var found = false;
if ( typeof obj[field] === 'object') {
found = deepIterate(obj[field], value)
if (found) { return true; }
}
}
return false;
};
return deepIterate(this, "some-value")
}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment