Commit 241707f9 authored by Jeremy Mikola's avatar Jeremy Mikola

Revise BSON documentation and fix references

parent 36983434
...@@ -13,27 +13,28 @@ BSON ...@@ -13,27 +13,28 @@ BSON
Overview Overview
-------- --------
MongoDB stores data records as BSON documents. BSON is a binary MongoDB stores data records as BSON documents. BSON is a binary representation
representation of :term:`JSON` documents, though it contains more data of :term:`JSON` documents, though it contains more data types than JSON. For the
types than JSON. For the BSON spec, see `bsonspec.org <http://bsonspec.org/>`_. BSON spec, see `bsonspec.org <http://bsonspec.org/>`_.
By default, the |php-library| returns BSON documents as By default, the |php-library| returns BSON documents as
``MongoDB\Model\BSONDocument`` objects and BSON arrays as :phpclass:`MongoDB\\Model\\BSONDocument` objects and BSON arrays as
``MongoDB\Model\BSONArray`` objects. ``MongoDB\Model\BSONDocument`` and :phpclass:`MongoDB\\Model\\BSONArray` objects.
``MongoDB\Model\BSONArray`` extend PHP's :phpclass:`BSONDocument <MongoDB\\Model\\BSONDocument>` and
:php:`ArrayObject <arrayobject>` class and implement the MongoDB PHP :phpclass:`BSONArray <MongoDB\\Model\\BSONArray>` extend PHP's
driver's :php:`MongoDB\\BSON\\Serializable <mongodb-bson-serializable>` :php:`ArrayObject <arrayobject>` class and implement the driver's
and :php:`MongoDB\\BSON\\Unserializable <mongodb-bson-unserializable>` :php:`MongoDB\\BSON\\Serializable <mongodb-bson-serializable>` and
interfaces. :php:`MongoDB\\BSON\\Unserializable <mongodb-bson-unserializable>` interfaces.
Type Maps Type Maps
--------- ---------
Most methods that read data from MongoDB support a ``typeMap`` option, Most methods that read data from MongoDB support a ``typeMap`` option, which
which allows control over how BSON is converted to PHP. Additionally, allows control over how BSON is converted to PHP. Additionally,
the :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and the :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
:phpclass:`MongoDB\\Collection` classes accept a ``typeMap`` option, :phpclass:`MongoDB\\Collection` classes accept a ``typeMap`` option, which can
which applies to any supporting methods and selected classes by default. be used to specify a default type map to apply to any supporting methods and
selected classes (e.g. :phpmethod:`MongoDB\\Client::selectDatabase()`).
The :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and The :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
:phpclass:`MongoDB\\Collection` classes use the following type map by :phpclass:`MongoDB\\Collection` classes use the following type map by
...@@ -47,20 +48,28 @@ default: ...@@ -47,20 +48,28 @@ default:
'root' => 'MongoDB\Model\BSONDocument', 'root' => 'MongoDB\Model\BSONDocument',
] ]
Serialization and deserialization of PHP variables into MongoDB
---------------------------------------------------------------
``Persistable`` Classes ``Persistable`` Classes
~~~~~~~~~~~~~~~~~~~~~~~ -----------------------
The driver's :php:`persistence specification <mongodb.persistence>` outlines how
classes implementing its :php:`MongoDB\\BSON\\Persistable
<mongodb-bson-persistable>` interface are serialized to and deserialized from
BSON. The :php:`Persistable <mongodb-bson-persistable>` interface is analogous
to PHP's :php:`Serializable interface <class.serializable>`.
The driver automatically handles serialization and deserialization for classes
implementing the :php:`Persistable <mongodb-bson-persistable>` interface without
requiring the use of the ``typeMap`` option. This is done by encoding the name
of the PHP class in a special property within the BSON document.
The PHP driver's :php:`persistence <mongodb.persistence>` specification .. note::
specifies how classes implementing :php:`MongoDB\\BSON\\Persistable
<mongodb-bson-persistable>` are serialized and deserialized, and is
analogous to PHP's :php:`Serializable interface <class.serializable>`.
The PHP :php:`driver <mongodb>` automatically handles serialization and When deserializing a PHP variable from BSON, the encoded class name of a
deserialization for classes implementing ``MongoDB\BSON\Persistable`` :php:`Persistable <mongodb-bson-persistable>` object will override any class
without requiring the use of the ``typeMap`` option. specified in the type map, but it will not override ``"array"`` and
``"stdClass"`` or ``"object"``. This is discussed in the
:php:`persistence specification <mongodb.persistence>` but it bears
repeating.
Consider the following class definition: Consider the following class definition:
...@@ -78,12 +87,9 @@ Consider the following class definition: ...@@ -78,12 +87,9 @@ Consider the following class definition:
{ {
$this->id = new MongoDB\BSON\ObjectID; $this->id = new MongoDB\BSON\ObjectID;
$this->name = (string) $name; $this->name = (string) $name;
$this->createdAt = new MongoDB\BSON\UTCDateTime;
// Get current time in milliseconds since the epoch
$msec = floor(microtime(true) * 1000);
$this->createdAt = new MongoDB\BSON\UTCDateTime($msec);
} }
function bsonSerialize() function bsonSerialize()
{ {
return [ return [
...@@ -92,7 +98,7 @@ Consider the following class definition: ...@@ -92,7 +98,7 @@ Consider the following class definition:
'createdAt' => $this->createdAt, 'createdAt' => $this->createdAt,
]; ];
} }
function bsonUnserialize(array $data) function bsonUnserialize(array $data)
{ {
$this->id = $data['_id']; $this->id = $data['_id'];
...@@ -101,7 +107,7 @@ Consider the following class definition: ...@@ -101,7 +107,7 @@ Consider the following class definition:
} }
} }
The following constructs a ``Person`` object, inserts it into the The following example constructs a ``Person`` object, inserts it into the
database, and reads it back as an object of the same type: database, and reads it back as an object of the same type:
.. code-block:: php .. code-block:: php
...@@ -149,51 +155,47 @@ The same document in the MongoDB shell might display as: ...@@ -149,51 +155,47 @@ The same document in the MongoDB shell might display as:
.. note:: .. note::
:php:`MongoDB\\BSON\\Persistable <mongodb-bson-persistable>` may only be used :php:`MongoDB\\BSON\\Persistable <mongodb-bson-persistable>` may only be used
for root and embedded BSON documents. BSON arrays are not supported. for root and embedded BSON documents. It may not be used for BSON arrays.
Emulating the Legacy Driver Emulating the Legacy Driver
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ---------------------------
The legacy :php:`mongo extension <mongo>` returned both BSON The legacy :php:`mongo extension <mongo>` returned both BSON documents and
documents and arrays as PHP arrays. While PHP arrays are convenient to arrays as PHP arrays. While PHP arrays are convenient to work with, this
work with, this behavior was problematic: behavior was problematic:
- Different BSON types could deserialize to the same PHP value (e.g. - Different BSON types could deserialize to the same PHP value (e.g.
``{"0": "foo"}`` and ``["foo"]``), which made it impossible to infer ``{"0": "foo"}`` and ``["foo"]``), which made it impossible to infer the
the original BSON type. original BSON type.
- Numerically-indexed PHP arrays would be serialized as BSON documents - Numerically-indexed PHP arrays would be serialized as BSON documents if there
if there was a gap in their key sequence. Such gaps were easily was a gap in their key sequence. Such gaps were easily caused by unsetting a
caused by unsetting a key to remove an element and key to remove an element and forgetting to numerically reindex the array.
forgetting to reindex the array.
The |php-library|'s ``MongoDB\Model\BSONDocument`` and ``MongoDB\Model\BSONArray`` The |php-library|'s :phpclass:`BSONDocument <MongoDB\\Model\\BSONDocument>` and
classes address these :phpclass:`BSONArray <MongoDB\\Model\\BSONArray>` classes address these concerns
concerns by preserving the BSON type information during serialization by preserving the BSON type information during serialization and
and deserialization; however, some users may still prefer the legacy deserialization; however, some users may still prefer the legacy behavior. If
behavior. If desired, you can use the ``typeMap`` option to have desired, you can use the ``typeMap`` option to have the library return
the library return everything as a PHP array: everything as a PHP array:
.. code-block:: php .. code-block:: php
<?php <?php
$client = new MongoDB\Client( $client = new MongoDB\Client(
null, 'mongodb://127.0.0.1/',
[], [],
['typeMap' => [ [
'root' => 'array', 'document' => 'array', 'array' => 'array' 'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'array',
], ],
] ]
); );
$document = $client->demo->zips->findOne( $document = $client->demo->zips->findOne(['_id' => '94301']);
['_id' => '94301'],
['typeMap' => [
'root' => 'array', 'document' => 'array', 'array' => 'array'
],
]
);
var_dump($document); var_dump($document);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment