1

I have tried to implement this in several ways and none seem to work. All I need to do is change the Status value of a specific Notifications object (found by its _id) from 0 to 1.

Example JSON:

{
    "_id": "577fbf0c7c6e88600ede5e73",
    "updatedAt": "2016-07-14T11:27:18.670Z",
    "createdAt": "2016-07-08T14:56:12.013Z",
    "Name": "Name",
    "Email": "[email protected]",
    "Notifications": [
      {
        "_id": "5787644108edec801e9cd0ab",
        "Title": "They commented on This",
        "Type": 1,
        "Status": 0,
        "TeamID": "578357109bb105602b1cba27",
        "DraftID": "578357b89bb105602b1cba2a"
      },
      {
        "_id": "578777167d1f3424251c361f",
        "Title": "He commented on That",
        "Type": 1,
        "Status": 0,
        "TeamID": "578357109bb105602b1cba27",
        "DraftID": "578357b89bb105602b1cba2a"
      }
    ]
    .....
  }

My route:

router.post('/notification', function (req, res) {
    UserProfile.update({
        'Notifications._id' : req.body.NotificationID
    }, {
        '$set' : {
            'Notifications.$.Status' : 1
        }
    }, function (err) {
        if (err)
            res.send(err);
    })
});

I don't get any errors and the update doesn't seem to happen. What could the problem be?

3 Answers 3

2

Is the type of Notifications._id ObjectId? If so, try cast req.body.NotificationId to ObjectId. Change the query to

{'Notifications._id' : mongoose.Types.ObjectId(req.body.NotificationID)}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes this worked! thank you very much. Do you have a reference link where this is explained? I'm new to this so I'd like to check it out
@Poot87 Good! In mongodb and mongoose, ObjectId type is different from String type. You can check the types of mongoose at http://mongoosejs.com/docs/api.html#schema_Schema.Types.
0

Try '$set' instead of $set. Ensure that you are getting id coming in correctly. Update callback comes with updated document - print that to know what happens.

2 Comments

Do you mean this? { ok: 1, nModified: 0, n: 0 }
add {'$new': true} as an argument after set - to check updated document
0

The $ is referring only the first item in the array. If you want to update all you have to use find and save:

UserProfile.findOne({
        'Notifications._id' : req.body.NotificationID
    }, function(err, doc) {
         _.find(doc.Notifications, {_id: req.body.NotificationID}).Status = 1;

         doc.save(function (err) {
        if (err)
            res.send(err);
    })
})

4 Comments

It's only meant to update one specific Notification object referenced by it's _id property.
ok but it won't work since you work on the whole doc. I'll fix my answer
Thank you for your answering. It was throwing this error : ReferenceError: _ is not defined
it's lodash. If you don't have it use .filter

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.