Right now, Mongo is skipping over this update because of a syntax error. And when I try different placements for the $.to. (like replies.$.to), this results: [MongoError: cannot use the part (to of replies.0.to.read.marked) to traverse the element..]
How should the $set be edited:
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 for userA of the specific reply:
{
"_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
}
}
]
}
],
...
}