the document is(I have simplified the structure):
{
"_id" : ObjectId("561f7896d3e4d2d8a6406baa"),
"array" : [
{
"array" : [
{
"taskId" : 1,
"finished" : false
}
]
}
]
}
I want to update the finished to true.
The query is
db.getCollection('JustTest').find({"array.array.taskId":1})
I tried db.getCollection('JustTest').update({"array.array.taskId":1},{$set:{"array.$.finished":true}})
.But the result is
{
"_id" : ObjectId("561f7896d3e4d2d8a6406baa"),
"array" : [
{
"array" : [
{
"taskId" : 1,
"finished" : false
}
],
"finished" : true
}
]
}
And "array.$.array.$.finished":true
is illegal and returns error msg.
I have read http://docs.mongodb.org/manual/reference/operator/update/positional/ .I also found a similar question How to update data in nested array in MongoDB but the answer is maybe naive.
I have no idea to update field in the nested array.Fix the position is not a good idea because the arrays(inner or outer) may be pushed or popped(from middle).making two collection can work but query will be difficult.