Lesson 1: Working with MongoDB Documents in PHP / Learn

Code Summary: Working with MongoDB Documents in PHP

The following is an example of a document in extended JSON:

{
  "_id": { "$oid": "5eb3d668b31de5d588f42974" },
  "borough": "Manhattan",
  "cuisine": "Italian",
  "name": "Marchis Restaurant"
}

The following demonstrates how to use an associative array to represent that document in PHP:

$document = [
  '_id' => new MongoDB\BSON\ObjectId('5eb3d668b31de5d588f42974'),
  'borough' => 'Manhattan',
  'cuisine' => 'Italian',
  'name' => 'Mariachis Restaurant',
];

This is the BSONDocument object of that same document:

object(MongoDB\Model\BSONDocument)#19 (1) {
["storage":"ArrayObject":private]=>
  array(4) {
    ["_id"]=>
      object(MongoDB\BSON\ObjectId)#24 (1) {
         ["oid"]=>
         string(24) "5eb3d668b31de5d588f42974"
      }
      ["borough"]=>
      string(9) "Manhattan"
      ["cuisine"]=>
      string(7) "Italian"
      ["name"]=>
      string(18) "Marchis Restaurant"
   }
}