you can try it using aggregate if you want to get only those notifications that has isRead field is false.
Model.User.aggregate([
{$match:{_id: userid}},
{$unwind:"$notification"},
{$match:{"notification.isRead": false}},
{$group:{_id:"$_id",notification:{ $addToSet: "$notification"}}}
]).exec(function (err, result) {
console.log(result);
res.send(result);
})
For example your document like:
{
"_id" : ObjectId("58454926668bde730a460e15"),
"notification" : [
{
"notification_id" : ObjectId("58454926668bde730a460e16"),
"isRead" : true
},
{
"notification_id" : ObjectId("58454926668bde730a460e17"),
"isRead" : true
},
{
"notification_id" : ObjectId("58454926668bde730a460e19"),
"isRead" : false
}
]
}
then out put will be like:
{
"_id" : ObjectId("58454926668bde730a460e15"),
"notification" : [
{
"notification_id" : ObjectId("58454926668bde730a460e19"),
"isReady" : false
}
]
}
If you want to get all notifications if any one of isRead is false then your query is correct just check userid is is exist in DB that you passed and some notification isRead is false . Also can use $elemMatch
Model.User.find({
_id: userid
"notification":{ $elemMatch: { "isRead": false} }
}).exec(function (err, result) {
console.log(result);
res.send(result);
})
isReadproperty, as inModel.User.find({ "notification.isRead": false }).exec(callback)? Do you get any results? If so, then check if the query with just theuseridworks, i.e.Model.User.find({ "_id": userid }).exec(callback). If that doesn't give you any result then it means theuseridyou specified does not exist and that potentially could be the reason why the combined filter is not working.