Last active
April 1, 2018 01:07
-
-
Save usametov/ed6d59da531fad0790b62f69b0c85f87 to your computer and use it in GitHub Desktop.
mongodb snippet: search for a value in all fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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