我试图用几种方式来实现这一点,而且都没有效果。 我需要做的是将特定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