title: MongoDB: Use save() to update an existing document in a collection
body: <p>everybody. I love manga. And now I am learning to love mongo too :-)</p>
  
  <p>There is an explanation of <a href="http://www.mongodb.org/display/DOCS/Updating+Data+in+Mongo" rel="noreferrer">how to use save() to update an existing document in a collection in MongoDB PHP</a>? but I couldn't apply it to the PHP's "reality" :-)</p>
  
  <pre><code>&gt; var mongo = db.things.findOne({name:"mongo"});
  &gt; print(tojson(mongo));
  {"_id" : "497dab624ee47b3a675d2d9c" , "name" : "mongo"}
  &gt; mongo.type = "database";
  database
  &gt; db.things.save(mongo);
  &gt; db.things.findOne({name:"mongo"});
  {"_id" : "497dab624ee47b3a675d2d9c" , "name" : "mongo" , "type" : "database"}
  </code></pre>
  
  <p>Here's my test code:</p>
  
  <pre><code>&lt;?php
  $a=array('_id'=&gt;'test_a','field1'=&gt;'anything');
  $b=array('_id'=&gt;'test_a','field2'=&gt;'anything else');
  
  $m=new Mongo();
  $c=$m-&gt;db-&gt;test;
  $c-&gt;save($a);
  $c-&gt;save($b);//overwrites the previous record
  
  /*
  //With update() it works fine
  $filter=array('_id'=&gt;'test_a');
  $update=array('$set'=&gt;array('field2'=&gt;'anything else'));
  $c-&gt;update($filter,$update);
  //$c-&gt;save($filter,$update);//Also tried...
  */
  
  $f=$c-&gt;find();
  echo $f-&gt;count()." found \n";
  $i=iterator_to_array($f);//mongo cursos iterator
  $m-&gt;close();//disconnect mongo
  
  print_r($i);
  ?&gt;
  </code></pre>
  
  <p>The fact is that in the PHP example save() overwrites the object while in the JS example it updates it. I wish I can reproduce in PHP the same as in JS.</p>
  
  <p>Thanks in advance.</p>