How to update nested object in mongodb
<p>I am rather new to mongodb and self learning. Since recently I am a bit stuck on this one. I would like to update a nested document. I worked around it by retrieving the full document and updating the entire thing, but I need a solution where I can update in a single shot. I made the following simplified example:</p>

<pre><code>db.test.insert({
    'name': 'someSchool',
    'classes': [
        {
            'name': 'someClass', 
            'students': [
                {
                    'name': 'Jack',
                    'coolness' : 7
                }
            ]
        }
    ]
});
</code></pre>

<p>Here the arrays only have one entry for simplicity, you understand we normally have more...</p>

<pre><code>db.test.find({
    'name' : 'someSchool', 
    'classes' : { 
        $elemMatch: {
            'name': 'someClass',
            'students.name' : 'Jack'
        }
    }
});
</code></pre>

<p>This find works! Thats great, so now I want to use it in an update, which is where Im stuck. Something like this??</p>

<pre><code>db.test.update({
    'name' : 'someSchool', 
    'classes' : { 
        $elemMatch: {
            'name': 'someClass',
            'students.name' : 'Jack'
        }
    }
}, {
    '$set' : { 'classes.students.coolness' : 9}
});
</code></pre>

<p>So this doesnt work and now I am stuck at it :) Hopefully someone here can help.</p>

<p>Side note: I am using mongoose. If you guys tell me its better to change the schema to make it easier somehow, I can.</p>

<p>Update since marked as duplicate: In the referenced question, there is only 1 level of nesting and the problem gets solved by using dot notation. Here there are 2 levels. I accepted teh answer which explains it is not supported and which gives a correct workaround.</p>