title: Updating nested arrays in mongodb
body: <p>I have a document in mongodb with 2 level deep nested array of objects that I need to update, something like this:</p>
  
  <pre><code>{
      id: 1,
      items: [
          {
              id: 2,
              blocks: [
                  {
                      id: 3
                      txt: 'hello'
                  }
              ]
          }
      ] 
  }
  </code></pre>
  
  <p>If there was only one level deep array I could use positional operator to update objects in it but for second level the only option I've came up is to use positional operator with nested object's index, like this:</p>
  
  <pre><code>db.objects.update({'items.id': 2}, {'$set': {'items.$.blocks.0.txt': 'hi'}})
  </code></pre>
  
  <p>This approach works but it seems dangerous to me since I'm building a web service and index number should come from client which can send say 100000 as index and this will force mongodb to create an array with 100000 indexes with null value.</p>
  
  <p>Are there any other ways to update such nested objects where I can refer to object's ID instead of it's position or maybe ways to check if supplied index is out of bounds before using it in query?</p>