title: how to rewrite Mongo client code using MongoDB driver instead
body: <p><strong>Background Information:</strong> </p>
  
  <p>I have a PHP script that parses a text file and uses it to insert data into a mongo database.  I'm moving away from the old Mongo PHP driver to use the MongoDB\Driver classes instead (<a href="http://php.net/manual/en/book.mongodb.php" rel="nofollow noreferrer">http://php.net/manual/en/book.mongodb.php</a>)</p>
  
  <p><strong>Problem</strong></p>
  
  <p>There's one section of code that I'm trying to rewrite using the new classes but I can't seem to get it working - aka its not writing data to the database... and I don't know how to find the error either.</p>
  
  <p><strong>Old Code:</strong></p>
  
  <p>This is the old code:</p>
  
  <pre><code> //database variables
   $m = new MongoClient("mongodb://127.0.0.1:27017");
  
   $db = "locations";
   function update_location_status($location)
   {
      global $m, $db;
      $collection = $db-&gt;locations;
      echo "&lt;pre&gt;".json_encode($status)."&lt;/pre&gt;";
  
      try {
          $cursor = $collection-&gt;update(array('domain'=&gt;$status['domain']) , array('$set'=&gt;$status), array('upsert'=&gt;true) ); 
          echo "Successfully upserted location status record for ". $location['domain'] ." into database...&lt;BR&gt;";
          return true;        
      } catch(MongoCursorException $e) {
          var_dump($cursor);
          return false;
      }
  </code></pre>
  
  <p><strong>New Code</strong></p>
  
  <pre><code>    $m = new \MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
      function update_location_status($location)
      {
      global $m;
      echo "&lt;pre&gt;".json_encode($status)."&lt;/pre&gt;";
      $bulk = new MongoDB\Driver\BulkWrite;
      $bulk-&gt;update(
          [array('domain'=&gt;$status['domain'])],
          [array('domain'=&gt;$status)],
          ['multi' =&gt; false, 'upsert' =&gt; true]
      );
  
      try{
          $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
          $result = $m-&gt;executeBulkWrite('db.locations', $bulk, $writeConcern);
          if($result-&gt;getInsertedCount()){
              return true;
          }else{
              echo "&lt;font color=red&gt;&lt;pre&gt;";
              var_dump($result-&gt;getWriteErrors());
              echo "&lt;/pre&gt;&lt;/font&gt;";
  
              return false;
          }
      } catch(MongoCursorException $e) {
          var_dump($result);
          return false;
      }
  </code></pre>
  
  <p><strong>Results</strong></p>
  
  <p>My attempt to dump the error message using <code>getWriteErrors</code> returns an empty array:</p>
  
  <pre><code>array(0) {
  }
  </code></pre>
  
  <p>Any suggestions would be appreciated. </p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>So I've updated the code and it's creating data, but it looks different from the previous / old code. 
  This is what the data from the old code looks like: </p>
  
  <pre><code>{ "_id" : ObjectId("584586312a0d63c2e21eee12"), 
      "bbn" : "133", 
      "customized" : true, 
      "nep" : "812", 
      "pdate" : ISODate("2017-02-05T06:44:41Z"), 
      "domain" : "test.mydomain", "status" : false, "t_name" : "agcab", "type" : "current_status" 
  }
  </code></pre>
  
  <p>And with this code: </p>
  
  <pre><code>global $m, $db;
  echo "&lt;pre&gt;".json_encode($status)."&lt;/pre&gt;";
  $bulk = new MongoDB\Driver\BulkWrite;
  $bulk-&gt;update(
      ['domain'=&gt;$status['domain']],
      ['$set'=&gt;['domain'=&gt;$status]],
      ['multi' =&gt; false, 'upsert' =&gt; true]
  );
  </code></pre>
  
  <p>the data ends up looking like:</p>
  
  <pre><code>{ "_id" : ObjectId("5910da5d99077b6b4887582f"), 
      "domain" : { 
          "bbn" : "133", 
          "nep" : "812", 
          "type" : "current_status", 
          "pdate" : ISODate("1970-01-18T07:00:12.882Z"), 
          "status" : false, 
          "domain" : "test.mydomain", 
          "tenant_name" : "agcab", 
          "customized" : true 
      } 
  }
  </code></pre>
  
  <p>I don't want it nested under another "domain" array.</p>