0

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.

Community
  • 1
  • 1
fairjm
  • 1,115
  • 12
  • 26
  • also related info: https://groups.google.com/forum/m/#!topic/mongodb-user/78Gqs_GWiYE – Tim Oct 15 '15 at 10:45
  • @chridam the answer use two fix position and only one `$` because the desc of question knows the first two position. :) – fairjm Oct 16 '15 at 01:26
  • @Tim Thank you but still no help.See the last line of my question. – fairjm Oct 16 '15 at 01:28
  • I know it's not what you want to hear, but there's still no good way to do this and the linked duplicates cover the options. – JohnnyHK Oct 16 '15 at 02:00
  • 1
    @JohnnyHK Thank you Johnny.I solved it by dimension reduction.I add an id field to inner array and remove the outer array so there is no nested array any more. : ) – fairjm Oct 16 '15 at 02:06

0 Answers0