title: mongodb push element into array of objects
body: <p>I have a collection with a following schema:</p>
  
  <pre><code>{
    "_id" : ObjectId("4ee3ddc346b3b8880a000000"),
    ...
    "msgs" : [{
        "mid" : ObjectId("4ee3ddc346b3b8880a000000"),
        "deleted" : []
      },
      {
        "mid" : ObjectId("4ee3ddc346b3b8880a000100"),
        "deleted" : []
      }]
  }
  </code></pre>
  
  <p>I want to do this query:
  for the specific _id insert into each "deleted" array an element </p>
  
  <p>so after doing this query I will receive something like this:</p>
  
  <pre><code>{
    "_id" : ObjectId("4ee3ddc346b3b8880a000000"),
    ...
    "msgs" : [{
        "mid" : ObjectId("4ee3ddc346b3b8880a000000"),
        "deleted" : [2]
      },
      {
        "mid" : ObjectId("4ee3ddc346b3b8880a000100"),
        "deleted" : [2]
      }]
  }
  </code></pre>
  
  <p>What I tried to do:</p>
  
  <pre><code>db.dialogs.update(
  { "_id" : ObjectId("4ee3ddc346b3b8880a000000")},
  {$addToSet : {'msgs.$.deleted' : 2}}
  )
  </code></pre>
  
  <p>the problem is that it only updating the first element element in the array, instead of all the elements</p>
  
  <p>Any ideas?</p>