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.

5
  • also related info: groups.google.com/forum/m/#!topic/mongodb-user/78Gqs_GWiYE Commented Oct 15, 2015 at 10:45
  • @chridam the answer use two fix position and only one $ because the desc of question knows the first two position. :) Commented Oct 16, 2015 at 1:26
  • @Tim Thank you but still no help.See the last line of my question. Commented Oct 16, 2015 at 1: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. Commented Oct 16, 2015 at 2: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. : ) Commented Oct 16, 2015 at 2:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.