目前,由于语法错误,Mongo正在跳过此更新。当我为$ .to尝试不同的展示位置时。 (比如回复。$。to),结果如下:[MongoError:不能使用部分(来自replies.0.to.read.marked)来遍历元素..]
如何编辑$ set:
Models.Message.findOneAndUpdate(
{ "replies._id": ObjectId("53dd4b67f0f23cad267f9d8b"), "replies.to.username": "UserA" },
{
"$set": {
"$.to.read.marked": true,
"$.to.read.datetime": req.body.datetimeRead,
"$.to.updated": req.body.datetimeRead
}
},
To $ set to.read.marked:true表示特定回复的用户A:
{
"_id" : ObjectId("53daf26af7b70fd82ffd4cff"),
"updated" : ISODate("2014-08-02T20:36:03.000Z"),
"message" : "here's a message",
"replies" : [
{
"reply" : "here's a reply",
"created" : 1407011687000,
"_id" : ObjectId("53dd4b67f0f23cad267f9d8b"),
"to" : [
{
"username" : "userA",
"updated" : ISODate("2014-08-02T20:34:47.000Z"),
"_id" : ObjectId("53dd4b67f0f23cad267f9d8c"),
"read" : {
"datetime" : ISODate("2014-08-02T20:34:47.000Z"),
"marked" : false
}
},
{
"username" : "userB",
"updated" : ISODate("2014-08-02T20:34:47.000Z"),
"_id" : ObjectId("53dd4b67f0f23cad267f9d8d"),
"read" : {
"datetime" : ISODate("2014-08-02T20:34:47.000Z"),
"marked" : false
}
}
]
},
{
"reply" : "here's another reply",
"created" : 1407011763000,
"_id" : ObjectId("53dd4bb3a9f9497e270525cb"),
"to" : [
{
"username" : "userA",
"updated" : ISODate("2014-08-02T20:36:03.000Z"),
"_id" : ObjectId("53dd4bb3a9f9497e270525cc"),
"read" : {
"datetime" : ISODate("2014-08-02T20:36:03.000Z"),
"marked" : false
}
},
{
"username" : "userB",
"updated" : ISODate("2014-08-02T20:36:03.000Z"),
"_id" : ObjectId("53dd4bb3a9f9497e270525cd"),
"read" : {
"datetime" : ISODate("2014-08-02T20:36:03.000Z"),
"marked" : false
}
}
]
}
],
...
}
答案 0 :(得分:1)
您不能将$
用于嵌套数组。因此,您根本无法使用update
。
关注this issue。
因此,您需要对find
进行replies._id
查询,然后继续手动获取索引/索引。
一个丑陋的选择是
Models.Message.findOne({ "replies._id":ObjectId("53dd4b67f0f23cad267f9d8b", "replies.to.username": "UserA")},
function(err,doc){
if(doc){
//use a for loop over doc.replies to find the index(index1) of ObjectId("53dd4b67f0f23cad267f9d8b") at replies[index]._id
var index1;
for(var i=0;i<doc.replies.length;i++){
if(doc.replies[i]._id === ObjectId("53dd4b67f0f23cad267f9d8b")){
index1=i;
break;
}
}
var index2;
//use a for loop over doc.replies[index1].to and find the index(index2) of "UserA" at replies[index1].to[index2]
for(var j=0;j<doc.replies[index1].to.length;j++){
if(doc.replies[index1].to[index2].username==="UserA"){
index2=j;
break;
}
}
doc.replies[index1].to[index2].read.marked = true;
doc.replies[index1].to[index2].read.datetime = req.body.datetimeRead;
doc.replies[index1].to[index2].updated= req.body.datetimeRead;
doc.markModified('replies')
doc.save(function(err,doc2){...})
}
});