我試圖用幾種方式來實現這一點,而且都沒有效果。 我需要做的是將特定Notifications
對象(由其_id
找到)的Status
值從0
更改爲1
。爲什麼我的Mongoose查詢不更新MongoDB數據庫中嵌套數組中的值?
例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"
}
]
.....
}
我的路線:
router.post('/notification', function (req, res) {
UserProfile.update({
'Notifications._id' : req.body.NotificationID
}, {
'$set' : {
'Notifications.$.Status' : 1
}
}, function (err) {
if (err)
res.send(err);
})
});
我沒有得到任何錯誤和更新似乎並沒有發生。 問題是什麼?
是的,這工作!非常感謝你。你有解釋這個參考鏈接嗎?我是新來的,所以我想看看它 – Poot87
你仍然會始終更新第一個通知 –
@ Poot87好!在mongodb和mongoose中,「ObjectId」類型與「String」類型不同。你可以在[http://mongoosejs.com/docs/api.html#schema_Schema.Types](http://mongoosejs.com/docs/api.html#schema_Schema.Types)檢查貓鼬的類型。 – wikrsh