To do this, you would have to iterate over each item that your cursor returns and traverse each document "manually".
This is not a mongo query! Note that my actual query is an empty find()
.
All I'm doing here is iterating over results and filtering out results that are not relevant.
var results = [];
db.data.find().forEach( function( doc ){
if ( find_name_recursive( doc ) ){
results.push( doc );
}
});
function find_name_recursive( doc ) {
for ( var attr in doc ) {
if ( doc.name != "undefined" && doc.name == "John" ){
return true;
}
if ( doc[ attr ] !== null && typeof( doc[ attr ] ) == "object" ) {
// drill down deeper into the object
find_name_recursive( doc[ attr ] );
}
}
}
Note that this is not tested code (don't have mongo on this machine). The majority of the recursive search function was taken from this post - Traverse all the Nodes of a JSON Object Tree with JavaScript
References -