Mongodb 无法在数组下的对象上设置值
我有一个这样的数据集Mongodb 无法在数组下的对象上设置值,mongodb,Mongodb,我有一个这样的数据集 { "_id" : ObjectId("5a45f6da3e6de20efc61ecc8"), "userid" : "5a20eb5bcdacc7086ce77427", "test" : [ { "sid" : "5a20ec53cdacc7086ce7742b", "sname" : "dev", "activity" : "Message",
{
"_id" : ObjectId("5a45f6da3e6de20efc61ecc8"),
"userid" : "5a20eb5bcdacc7086ce77427",
"test" : [
{
"sid" : "5a20ec53cdacc7086ce7742b",
"sname" : "dev",
"activity" : "Message",
"isread" : 0,
"timestamp" : 1514535070925
},
{
"sid" : "5a20ec53cdacc7086ce7742b",
"sname" : "dev",
"activity" : "Message",
"isread" : 0,
"timestamp" : 1514535356213
}
],
"__v" : 0
}
现在我无法将“isread”值从0修改为1。
我写的是:
db.Collection.update({'userid': req.body.userid, 'notifications.timestamp': '1514535356213'}, {$set:{'isread': parseInt(1)}}, {new: true}, function(error, result) {
if(error) {
console.log(error);
} else {
console.log(result);
//I am getting this result i.e {
"ok": 0,
"n": 0,
"nModified": 0
}
}
});
我不知道为什么我的值没有更新。
非常感谢您的帮助请尝试
> db.coll3.update({
'userid': '5a20eb5bcdacc7086ce77427'
}, {
$set: {
'test.$[elem].isread': 1
}
}, {
multi: true,
new: true,
arrayFilters: [{
'elem.timestamp': {
$eq: 1514535356213
}
}
]
})
WriteResult({
"nMatched": 1,
"nUpserted": 0,
"nModified": 1
})
使用findAndModify
db.coll3.findAndModify({query:{
'userid': '5a20eb5bcdacc7086ce77427'
}, update:{
$set: {
'test.$[elem].isread': 1
}
},
new: true,
arrayFilters: [{
'elem.timestamp': {
$eq: 1514535356213
}
}
]
})
有多种方法可以执行此类操作。希望这有帮助我注意到您在查询中使用了-notifications.timestamp??您遇到问题了吗?请尝试
db.Collection.update({'userid':req.body.userid,'test.timestamp':15145356213},{'set':{'test.$.isread':1},{'new':true},回调)