My data is as following:
{
"profileID": "123",
"fullname": "Name",
"notifications": [
{
"read": false,
"date": "2017-05-06 13:40:01",
"post": "5555555",
"action": "commented",
"profileID": "456"
},
{
"read": false,
"date": "2017-05-06 13:40:15",
"post": "5555555",
"action": "commented",
"profileID": "456"
}
]}
I am trying to make a Node API route to be able to update each notification. For the uniqueness, the date variable can be used.
So to summarise:
- Find user by profileID
- Get his/her notifications
- Update the read value for the notification that matches the date.
I have constructed it in the following way:
apiRouter.post('/api/changeNotificationStatus', function(req, res){
userModel.update(
{profileID: req.body. profileID, "notifications.date": req.body.date, "notifications.post": req.body.post, "notifications.action": req.body.action},
{$set:{"notifications.$.read": true}},
{multi: false},
function(err, data) {
if (err){
console.log(err);
} else {
console.log(data);
}
});
});
There aren't any errors but I get the following:
{ n: 0, nModified: 0, ok: 1 }
I have confirmed that the variables: req.body.profileID, req.body.date, req.body.date, req.body.post and req.body.action are coming through just fine.
Am I doing something wrong?
PS: Thanks Neil Lunn for pointing me to the model.update post!