Update embedded object inside array inside array in MongoDB
mongodb, mongodb-query
Solution
You can only use the `$` positional operator for single-level arrays. In your case, you have a nested array (`heros` is an array, and within that each hero has a `spells` array).
If you know the indexes of the arrays, you can use explicit indexes when doing an update, like:
> db.test.update({"heros.nickname":"test", "heros.spells.spell_id":1}, {$set:{"heros.0.spells.1.level":3}});
Problem
I have document like ``` { id : 100, heros:[ { nickname : "test", spells : [ {spell_id : 61, level : 1}, {spell_id : 1, level : 2} ] } ] } ``` I can't `$set` spell's `level : 3` with `spell_id : 1` inside `spells` that inside `heros` with nickname "test. I tried this query: ``` db.test.update({"heros.nickname":"test", "heros.spells.spell_id":1}, {$set:{"heros.spells.$.level":3}}); ``` Errror i see is can't append to array using string field name [spells] Thanks for help.