Mongodb Model.update不适用于Mongoose
我的数据如下:Mongodb Model.update不适用于Mongoose,mongodb,mongoose,Mongodb,Mongoose,我的数据如下: { "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-0
{
"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"
}
]}
{ n: 0, nModified: 0, ok: 1 }
我正在尝试创建一个节点API路由,以便能够更新每个通知。为了唯一性,可以使用日期变量
因此,总结如下:
- 通过profileID查找用户
- 获取他/她的通知
- 更新与日期匹配的通知的读取值
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);
}
});
});
没有任何错误,但我得到以下信息:
{
"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"
}
]}
{ n: 0, nModified: 0, ok: 1 }
我已经确认变量:req.body.profileID、req.body.date、req.body.date、req.body.post和req.body.action都很顺利
我做错什么了吗
PS:谢谢Neil Lunn给我指点模特。更新帖子 为了帮助任何遇到这种情况的人,存在多个问题: 1) userModel的架构错误,如中所示,它应该是:
notifications: [{
read: Boolean,
date: Date,
post: String,
action: String,
profileID: String
}]
而不是:
notifications: [{ }]
req.body.date
2) 在userModel.update命令中,我应该执行以下操作:
new Date(req.body.date)
而不是:
notifications: [{ }]
req.body.date
希望这对将来的人有所帮助。@neil lunn-我已经更新了这个问题,所以它不再是重复的,仍然是重复的。你的语法错了。再读一遍答案。这是更正<代码>{profileID:req.body.profileID,“notifications.date”:req.body.date,“notifications.post”:req.body.post,“notifications.action”:req.body.action}。因此,在更新的“查询”部分中不使用
$
。您只在$set
之后的部分使用它。感谢您指出它,并为愚蠢的错误感到抱歉。但是,我只是尝试了一下,得到了相同的结果,即:{n:0,nModified:0,ok:1}。。。我再次检查了参数(通过调试器),它们看起来很好。有什么想法吗?我猜这个问题不再重复了n:0
表示您的update
查询没有找到要更新的文档。