2

I have a schema design like this

var userNotificationSchema = new Schema({
    notification_id: { type: Schema.Types.ObjectId, ref: 'notifications' },
    isRead: { type: Boolean }   
});

var userSchema = new Schema({
    notification: [userNotificationSchema]
});

I want to fetch all notifications array list which isRead: 'false'.

For that I wrote

Model.User.find({
    _id: userid,
    'notification.isRead': false 
}, function (err, result) { 
    console.log(result);
    res.send(result);
});

but this returns [] as result.

4
  • what is your mongoose version used in the app? Commented Jan 3, 2017 at 9:55
  • mongoose version 4.6.1 Commented Jan 3, 2017 at 9:59
  • query looks correct, is userid is correct for _id? Commented Jan 3, 2017 at 10:01
  • Have you tried querying with just the isRead property, as in Model.User.find({ "notification.isRead": false }).exec(callback)? Do you get any results? If so, then check if the query with just the userid works, i.e. Model.User.find({ "_id": userid }).exec(callback). If that doesn't give you any result then it means the userid you specified does not exist and that potentially could be the reason why the combined filter is not working. Commented Jan 3, 2017 at 10:20

2 Answers 2

1

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);
})
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand why negative and can you tell me ?
0

My guess is your reference is wrong because the query is correct.

Make sure you are referring what you exporting.

Example:

If you are referring notifications in your schema code then you should export the same name in code.

module.exports = mongoose.model("notifications", userNotificationSchema);

Take a look at here https://alexanderzeitler.com/articles/mongoose-referencing-schema-in-properties-and-arrays/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.