example-insertOne.rst 1.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
.. code-block:: php
                
   <?php
   
   $database = new MongoDB\Client;

   $collection = $database->selectCollection('users','restaurants');

   $newUser = $collection->insertOne(
       [
           'username' => 'admin',
           'email' => 'admin@example.com',
           'name' => 'Anna Bennett'
       ],
   );

   echo "<pre>";
   var_dump($newUser);

The output would resemble:

.. code-block:: none
                
   object(MongoDB\InsertOneResult)#13 (3) {
     ["writeResult":"MongoDB\InsertOneResult":private]=>
     object(MongoDB\Driver\WriteResult)#12 (9) {
       ["nInserted"]=>
       int(1)
       ["nMatched"]=>
       int(0)
       ["nModified"]=>
       int(0)
       ["nRemoved"]=>
       int(0)
       ["nUpserted"]=>
       int(0)
       ["upsertedIds"]=>
       array(0) {
       }
       ["writeErrors"]=>
       array(0) {
       }
       ["writeConcernError"]=>
       NULL
       ["writeConcern"]=>
       array(4) {
         ["w"]=>
         NULL
         ["wmajority"]=>
         bool(false)
         ["wtimeout"]=>
         int(0)
         ["journal"]=>
         NULL
       }
     }
     ["insertedId":"MongoDB\InsertOneResult":private]=>
     object(MongoDB\BSON\ObjectID)#11 (1) {
       ["oid"]=>
       string(24) "577282631f417d1823121691"
     }
     ["isAcknowledged":"MongoDB\InsertOneResult":private]=>
     bool(true)
   }