title: laravel mongodb push element to existing array in document_
body: <p>In my mongodb collection I want to push some elements to an existing array. I use <a href="https://github.com/jenssegers/Laravel-MongoDB" rel="nofollow">jenssegers/Laravel-MongoDB - Eloquent model and Query builder</a> to work with lavavel and mongodb. </p>
  
  <p><strong>How can I use the $push operator in jenssegers/Laravel-MongoDB?</strong></p>
  
  <h3>MongoDB entry which shall be updated (RockMongo representation):</h3>
  
  <pre class="lang-js prettyprint-override"><code>{
     "_id": ObjectId("5328bc2627784fdb1a6cd398"),
     "comments": {
       "0": {
         "id": 3,
         "score": 8
      }
    },
     "created_at": ISODate("2014-03-18T21:35:34.0Z"),
     "file": {
       "file_id": NumberLong(1175),
       "timestamp": NumberLong(1395178534)
    }
  }
  </code></pre>
  
  <hr>
  
  <h3>Hint about array representation in rockmongo and mongo shell</h3>
  
  <p><a href="http://rockmongo.com" rel="nofollow">RockMongo</a> and <a href="http://docs.mongodb.org/manual/administration/scripting/" rel="nofollow">mongo shell</a> array representation of the documents are a little bit different. Have a look at the <code>comments</code>-array. The above RockMongo representation appears in the mongo shell as: </p>
  
  <pre class="lang-js prettyprint-override"><code>{ 
      "_id" : ObjectId("5328c33a27784f3b096cd39b"), 
      "comments" : [  
       {  
          "id" : 3,  
          "score" : 8 
       } 
      ], 
      "created_at" : ISODate("2014-03-18T22:05:46Z"), 
      "file" : { 
          "file_id" : NumberLong(1176), 
          "timestamp" : NumberLong(1395180346) 
      } 
  }
  </code></pre>
  
  <hr>
  
  <p>As the documentation states the <code>$push</code> operater to push elements to an array. This works fine in the mongo-shell: </p>
  
  <h3>Mongo shell</h3>
  
  <pre class="lang-sh prettyprint-override"><code>db.Images.update({'file.file_id': 1175}, 
     { $push: { comments: { id: 3, score: 8} } 
  })
  </code></pre>
  
  <p>But in the query-builder I struggle to incorporate the $push operator. I get the error: </p>
  
  <pre><code>localhost:27017: Modified field name may not start with $
  </code></pre>
  
  <p>I did not find any documentation or example that showed me how to do it.. </p>
  
  <h3>My jenssegers/Laravel-MongoDB code, that returns the error</h3>
  
  <pre class="lang-php prettyprint-override"><code>// $file_id = 1175
  public static function addComment( $file_id ) {  
      $image = Images::where( 'file.file_id', '=', floatval( $file_id ) )
      -&gt;update( array('$push' =&gt; array( 'comments' =&gt; array( 'id' =&gt; 4, 'score' =&gt; 9 ) ) ) );
      return $image; 
  } 
  </code></pre>