title: Updating multiple embedded docs in mongoDB
body: <p>I need to update multiple embedded docs in mongo using PHP. My layout looks like this:</p>
  
  <pre><code>{
    _id: id,
    visits : {
          visitID: 12
          categories: [{
                catagory_id: 1,
                name: somename,
                count: 11,
                duration: 122
                },
                {
                catagory_id: 1,
                name: some other name,
                count: 11,
                duration: 122
                },
                {
                catagory_id: 2,
                name: yet another name,
                count: 11,
                duration: 122
                }]
     }
  }
  </code></pre>
  
  <p>The document can have more than one visit too.</p>
  
  <p>Now i want to update 2 categories, one with <code>id=1</code> and <code>name=somename</code> and the other with <code>id=1</code> and <code>name=some_new_name</code>. Both of them should inc "count" by 1 and "duration" by 45.</p>
  
  <p>First document exists, but second does not. </p>
  
  <p>Im thinking of having a function like this:</p>
  
  <pre><code>function updateCategory($id, $visitID,$category_name,$category_id) {
  
      $this-&gt;profiles-&gt;update(
              array(
                  '_id' =&gt; $id, 
                  'visits.visitID' =&gt; $visitID,
                  'visits.categories.name' =&gt; $category_name,
                  'visits.categories.id' =&gt; $category_id,
              ), 
              array(
                  '$inc' =&gt;   array(
                              'visits.categories.$.count' =&gt; 1,
                              'visits.categories.$.duration' =&gt;45,
                          ),
              ), 
              array("upsert" =&gt; true)
      );  
  }   
  </code></pre>
  
  <p>But with this i need to call the function for each category i will update. Is there any way to do this in one call?</p>
  
  <p>EDIT:</p>
  
  <p>Changed the layout a bit and made "categories" an object instead of array. Then used a combination of "category_id" and "category_name" as property name. Like:</p>
  
  <pre><code>categories: {
                1_somename : {
                    count: 11,
                    duration: 122
                },
                1_some other name : {
                    count: 11,
                    duration: 122
                },
                2_yet another name : {
                    count: 11,
                    duration: 122
                },
  }
  </code></pre>
  
  <p>Then with upsert and something like </p>
  
  <pre><code>$inc: {
   "visits.$.categories.1_somename.d": 100,
   "visits.$.categories.2_yet another name.c": 1
  }
  </code></pre>
  
  <p>i can update several "objects" at a time..  </p>