I am trying to push a notification object to a mongoDB array with the follow condition:
Check if there is a notification inside the array with the same postId If yes: return that object If no: create a new notification object
I'm using node with Mongoose ODM. In some other questions online they give this as an answer:
user.update({'notifications.postId': {$ne: post._id}}, {$push: { 'notifications': {type: 'POST', postId: post._id}}}, function(err, doc) {
.....
});
But this didn't work for me.
Below you find the structure of the mongodb document.
{
"_id": {
"$oid": "56631b22067ee41847dab6bb"
},
"unreadNotifications": 0,
"notifications": [
{
"type": "POST",
"postId": {
"$oid": "5666b78b61eb95bc03d47a71"
},
"_id": {
"$oid": "56782daa395840efd4ed3f7a"
},
"createdAt": {
"$date": "2015-12-21T16:49:46.903Z"
},
"events": []
}
]
"created": {
"$date": "2015-12-05T17:13:06.782Z"
},
"lastName": "Power",
"firstName": "John",
"__v": 0
}