Skip to content

Commit eda7d51

Browse files
committed
Revise BSON documentation and fix references
1 parent f58ab31 commit eda7d51

File tree

1 file changed

+62
-60
lines changed

1 file changed

+62
-60
lines changed

docs/reference/bson.txt

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ BSON
1313
Overview
1414
--------
1515

16-
MongoDB stores data records as BSON documents. BSON is a binary
17-
representation of :term:`JSON` documents, though it contains more data
18-
types than JSON. For the BSON spec, see `bsonspec.org <http://bsonspec.org/>`_.
16+
MongoDB stores data records as BSON documents. BSON is a binary representation
17+
of :term:`JSON` documents, though it contains more data types than JSON. For the
18+
BSON spec, see `bsonspec.org <http://bsonspec.org/>`_.
1919

2020
By default, the |php-library| returns BSON documents as
21-
``MongoDB\Model\BSONDocument`` objects and BSON arrays as
22-
``MongoDB\Model\BSONArray`` objects. ``MongoDB\Model\BSONDocument`` and
23-
``MongoDB\Model\BSONArray`` extend PHP's
24-
:php:`ArrayObject <arrayobject>` class and implement the MongoDB PHP
25-
driver's :php:`MongoDB\\BSON\\Serializable <mongodb-bson-serializable>`
26-
and :php:`MongoDB\\BSON\\Unserializable <mongodb-bson-unserializable>`
27-
interfaces.
21+
:phpclass:`MongoDB\\Model\\BSONDocument` objects and BSON arrays as
22+
:phpclass:`MongoDB\\Model\\BSONArray` objects.
23+
:phpclass:`BSONDocument <MongoDB\\Model\\BSONDocument>` and
24+
:phpclass:`BSONArray <MongoDB\\Model\\BSONArray>` extend PHP's
25+
:php:`ArrayObject <arrayobject>` class and implement the driver's
26+
:php:`MongoDB\\BSON\\Serializable <mongodb-bson-serializable>` and
27+
:php:`MongoDB\\BSON\\Unserializable <mongodb-bson-unserializable>` interfaces.
2828

2929
Type Maps
3030
---------
3131

32-
Most methods that read data from MongoDB support a ``typeMap`` option,
33-
which allows control over how BSON is converted to PHP. Additionally,
32+
Most methods that read data from MongoDB support a ``typeMap`` option, which
33+
allows control over how BSON is converted to PHP. Additionally,
3434
the :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
35-
:phpclass:`MongoDB\\Collection` classes accept a ``typeMap`` option,
36-
which applies to any supporting methods and selected classes by default.
35+
:phpclass:`MongoDB\\Collection` classes accept a ``typeMap`` option, which can
36+
be used to specify a default type map to apply to any supporting methods and
37+
selected classes (e.g. :phpmethod:`MongoDB\\Client::selectDatabase()`).
3738

3839
The :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
3940
:phpclass:`MongoDB\\Collection` classes use the following type map by
@@ -47,20 +48,28 @@ default:
4748
'root' => 'MongoDB\Model\BSONDocument',
4849
]
4950

50-
Serialization and deserialization of PHP variables into MongoDB
51-
---------------------------------------------------------------
52-
5351
``Persistable`` Classes
54-
~~~~~~~~~~~~~~~~~~~~~~~
52+
-----------------------
53+
54+
The driver's :php:`persistence specification <mongodb.persistence>` outlines how
55+
classes implementing its :php:`MongoDB\\BSON\\Persistable
56+
<mongodb-bson-persistable>` interface are serialized to and deserialized from
57+
BSON. The :php:`Persistable <mongodb-bson-persistable>` interface is analogous
58+
to PHP's :php:`Serializable interface <class.serializable>`.
59+
60+
The driver automatically handles serialization and deserialization for classes
61+
implementing the :php:`Persistable <mongodb-bson-persistable>` interface without
62+
requiring the use of the ``typeMap`` option. This is done by encoding the name
63+
of the PHP class in a special property within the BSON document.
5564

56-
The PHP driver's :php:`persistence <mongodb.persistence>` specification
57-
specifies how classes implementing :php:`MongoDB\\BSON\\Persistable
58-
<mongodb-bson-persistable>` are serialized and deserialized, and is
59-
analogous to PHP's :php:`Serializable interface <class.serializable>`.
65+
.. note::
6066

61-
The PHP :php:`driver <mongodb>` automatically handles serialization and
62-
deserialization for classes implementing ``MongoDB\BSON\Persistable``
63-
without requiring the use of the ``typeMap`` option.
67+
When deserializing a PHP variable from BSON, the encoded class name of a
68+
:php:`Persistable <mongodb-bson-persistable>` object will override any class
69+
specified in the type map, but it will not override ``"array"`` and
70+
``"stdClass"`` or ``"object"``. This is discussed in the
71+
:php:`persistence specification <mongodb.persistence>` but it bears
72+
repeating.
6473

6574
Consider the following class definition:
6675

@@ -78,12 +87,9 @@ Consider the following class definition:
7887
{
7988
$this->id = new MongoDB\BSON\ObjectID;
8089
$this->name = (string) $name;
81-
82-
// Get current time in milliseconds since the epoch
83-
$msec = floor(microtime(true) * 1000);
84-
$this->createdAt = new MongoDB\BSON\UTCDateTime($msec);
90+
$this->createdAt = new MongoDB\BSON\UTCDateTime;
8591
}
86-
92+
8793
function bsonSerialize()
8894
{
8995
return [
@@ -92,7 +98,7 @@ Consider the following class definition:
9298
'createdAt' => $this->createdAt,
9399
];
94100
}
95-
101+
96102
function bsonUnserialize(array $data)
97103
{
98104
$this->id = $data['_id'];
@@ -101,7 +107,7 @@ Consider the following class definition:
101107
}
102108
}
103109

104-
The following constructs a ``Person`` object, inserts it into the
110+
The following example constructs a ``Person`` object, inserts it into the
105111
database, and reads it back as an object of the same type:
106112

107113
.. code-block:: php
@@ -149,51 +155,47 @@ The same document in the MongoDB shell might display as:
149155
.. note::
150156

151157
:php:`MongoDB\\BSON\\Persistable <mongodb-bson-persistable>` may only be used
152-
for root and embedded BSON documents. BSON arrays are not supported.
158+
for root and embedded BSON documents. It may not be used for BSON arrays.
153159

154160
Emulating the Legacy Driver
155-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
161+
---------------------------
156162

157-
The legacy :php:`mongo extension <mongo>` returned both BSON
158-
documents and arrays as PHP arrays. While PHP arrays are convenient to
159-
work with, this behavior was problematic:
163+
The legacy :php:`mongo extension <mongo>` returned both BSON documents and
164+
arrays as PHP arrays. While PHP arrays are convenient to work with, this
165+
behavior was problematic:
160166

161-
- Different BSON types could deserialize to the same PHP value (e.g.
162-
``{"0": "foo"}`` and ``["foo"]``), which made it impossible to infer
163-
the original BSON type.
167+
- Different BSON types could deserialize to the same PHP value (e.g.
168+
``{"0": "foo"}`` and ``["foo"]``), which made it impossible to infer the
169+
original BSON type.
164170

165-
- Numerically-indexed PHP arrays would be serialized as BSON documents
166-
if there was a gap in their key sequence. Such gaps were easily
167-
caused by unsetting a key to remove an element and
168-
forgetting to reindex the array.
171+
- Numerically-indexed PHP arrays would be serialized as BSON documents if there
172+
was a gap in their key sequence. Such gaps were easily caused by unsetting a
173+
key to remove an element and forgetting to numerically reindex the array.
169174

170-
The |php-library|'s ``MongoDB\Model\BSONDocument`` and ``MongoDB\Model\BSONArray``
171-
classes address these
172-
concerns by preserving the BSON type information during serialization
173-
and deserialization; however, some users may still prefer the legacy
174-
behavior. If desired, you can use the ``typeMap`` option to have
175-
the library return everything as a PHP array:
175+
The |php-library|'s :phpclass:`BSONDocument <MongoDB\\Model\\BSONDocument>` and
176+
:phpclass:`BSONArray <MongoDB\\Model\\BSONArray>` classes address these concerns
177+
by preserving the BSON type information during serialization and
178+
deserialization; however, some users may still prefer the legacy behavior. If
179+
desired, you can use the ``typeMap`` option to have the library return
180+
everything as a PHP array:
176181

177182
.. code-block:: php
178183

179184
<?php
180185

181186
$client = new MongoDB\Client(
182-
null,
187+
'mongodb://127.0.0.1/',
183188
[],
184-
['typeMap' => [
185-
'root' => 'array', 'document' => 'array', 'array' => 'array'
189+
[
190+
'typeMap' => [
191+
'array' => 'array',
192+
'document' => 'array',
193+
'root' => 'array',
186194
],
187195
]
188196
);
189197

190-
$document = $client->demo->zips->findOne(
191-
['_id' => '94301'],
192-
['typeMap' => [
193-
'root' => 'array', 'document' => 'array', 'array' => 'array'
194-
],
195-
]
196-
);
198+
$document = $client->demo->zips->findOne(['_id' => '94301']);
197199

198200
var_dump($document);
199201

0 commit comments

Comments
 (0)