title: PHP-MONGO How to update a particular field
body: <p>I'm trying to develop a RESTful PHP API based on MongoDB. I make a POST passing a JSON body like this:</p>
  
  <pre><code>{"id":"1","timestamp":"blah blah","value:"blah"}
  </code></pre>
  
  <p>Then in the collection called "device" I want to create a structure like this:</p>
  
  <pre><code>{ _id: 1
  alt: blah
  lng : blah
  senseddata: {
      timestamp: blah
      value :blah
      }
      {
      timestamp: blah blah
      value: blah
      }
      .
      .
      .
  }
  </code></pre>
  
  <p>In other words, I just only want to update "senseddata" field. So I wrote this query in Mongo Shell:</p>
  
  <pre><code>db.device.update({"_id":1},{$push:{"senseddata":{"timestamp":1234,"value":"12"}}})
  </code></pre>
  
  <p>and it works perfectly. Now I tried to do the same in PHP. I wrote this:</p>
  
  <pre><code>$id = $data['id'];
  $timestamp = $data['timestamp'];
  $value = $data['value'];
  $collection = $db-&gt;device;
  $collection-&gt;update(array("_id" =&gt; $id),
                           array('$push' =&gt; array('senseddata' =&gt; array('timespamp' =&gt; $timestamp,'value' =&gt; $value)))
                     );
  </code></pre>
  
  <p>and it didn't work. Then I tried using '$set' and I noticed a strange behaviour: if I leave my code as it is, it doesn't work. But if I write like that:</p>
  
  <pre><code>//$id = $data['id'];
  $timestamp = $data['timestamp'];
  $value = $data['value'];
  $id = 1;
  $collection = $db-&gt;device;
  $collection-&gt;update(array("_id" =&gt; $id),
                           array('$push' =&gt; array('senseddata' =&gt; array('timespamp' =&gt; $timestamp,'value' =&gt; $value)))
                     );
  </code></pre>
  
  <p>it works (i didn't understand why) but it changes (as expected) entirely the structure of my collection. And it's not what I want. Any suggestion? Thanks in advance.</p>