How can I perform an in-depth regex search in MongoDB using a JavaScript function within the query? Here's an example:
db.collection.find(
{$where: function() {
function deepIterate(obj, value) {
var new_value = new RegExp("^"+value, "i");
for (var field in obj) {
if (obj[field] == new_value){
return true;
}
var found = false;
if (typeof obj[field] === 'object') {
found = deepIterate(obj[field], new_value)
if (found) { return true; }
}
}
return false;
};
return deepIterate(this, "[A-Ba-b0-9]*60.51[A-Ba-b0-9]*")
}}
)
However, the current query is not returning any values and outputting Fetched 0 record(s) in 4ms
. Despite having a record in the database with the string 192.108.60.51
, it is not being returned. Any help on this issue would be greatly appreciated!