title: How to execute JS code on MongoDB using PHP
body: <p>How can I execute a small piece of JS code on MongoDB using PHP and preferably using the laravel-mongodb driver, but that is not a hard requirement.</p>
  
  <pre><code>var size = 0;
  db.mycollection.find( { project_id: 151 } ).forEach(function(obj) {
      var curr = Object.bsonsize(obj); 
      size = size + curr;
  })
  print(size);
  </code></pre>
  
  <p>It does not matter if this needs to be transformed to a stored function, as long as it is able to run on a shared MongoDB and the project ID (in this case 151) is not fixed. And if somebody knows a better function or solution thats also OK with me, as long as I can calculate the size of all objects for a given project_id.</p>
  
  <p>Any toughts on the matter?</p>