javascript - How to find if element exists in document - MongoDB? -


i trying check collection return documents element of array in document value.

for example, here 1 of documents json:

[ {     "employer" : "employer1@gmail.com",     "applicants" : [         {             "email" : "joe@gmail.com"         },         {             "email" : "fred@gmail.com"         }     ] }, {     "employer" : "employer2@gmail.com",     "applicants" : [         {             "email" : "steven@gmail.com"         }     ] }, {     "employer" : "employer3@gmail.com", } ] 

now, returned document applicants array not contain email "steven@gmail.com" or there no applicants array.

in example json above, should return documents employer1 , employer2.

i have gotten far return documents applicant steven@gmail.com not exist, employer1, don't understand how can include employer3. how can this?

this query far in javascript:

collection.find({"applicants.email" : {$ne : req.user.username}}, {}, function(e,docs){             res.json(docs);             console.log(docs);     }); 

try following query, may you:-

refer $size more info on how use it.

this query return results, if applicants.email not equal req.user.username or if applicants array size 0.

collection.find({$or:[                      {"applicants.email" : {$ne : req.user.username}},                      {applicants: {$size: 0}}                  }], function(e,docs){         console.log(docs); }); 

i never tried query. let me know if giving error.

also can try out one:-

this query return results, if applicants.email not equal req.user.username or if applicants array not present.

 collection.find({$or:[                      {"applicants.email" : {$ne : req.user.username}},                      {applicants: {$exists: false}}                  }], function(e,docs){         console.log(docs); }); 

refer $exists more info.

hope help.