Search code examples
mongodbembedded-documents

Update a complex schema with sub embed document


i m trying to update a sub embed document with mongodb, but does not work.

I want for a chapter.id & quest.id given, update the percent of this document (only this one)

My schema looks like :

{
    "_id" : ObjectId("54ae5e63e71cef26050fca9a"),
    "name" : "test",
    "chapters" : [ 
        {
            "id" : 1,
            "quests" : [ 
                {
                    "id" : 1,
                    "name" : "quest1",
                    "percent" : 0
                }, 
                {
                    "id" : 2,
                    "name" : "quest2",
                    "percent" : 0
                }
            ]
        }, 
        {
            "id" : 2,
            "quests" : [ 
                {
                    "id" : 3,
                    "name" : "quest3",
                    "percent" : 0
                }, 
                {
                    "id" : 4,
                    "name" : "quest4",
                    "percent" : 0
                }
            ]
        }
    ]
}

i tried this :

db.test.update({name:"test"  , "chapters.quests.id":1 , "chapters.id":1} , {$set:{ "chapters.quests.$.percent":1} })

and has this error

cannot use the part (chapters of chapters.quests.0.percent) to traverse the element ({chapters: [ { id: 1.0, quests: [ { id: 1.0, name: "quest1", percent: 0.0 }, { id: 2.0, name: "quest2", percent: 0.0 } ] }, { id: 2.0, quests: [ { id: 3.0, name: "quest3", percent: 0.0 }, { id: 4.0, name: "quest4", percent: 0.0 } ] } ]})

if i did it with 0 if someone can help me, it s perhaps a schema problems ?


Solution

  • Now it is not possible without know the position of the object.