13
13
Overview
14
14
--------
15
15
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/>`_.
19
19
20
20
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.
28
28
29
29
Type Maps
30
30
---------
31
31
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,
34
34
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()`).
37
38
38
39
The :phpclass:`MongoDB\\Client`, :phpclass:`MongoDB\\Database`, and
39
40
:phpclass:`MongoDB\\Collection` classes use the following type map by
@@ -47,20 +48,28 @@ default:
47
48
'root' => 'MongoDB\Model\BSONDocument',
48
49
]
49
50
50
- Serialization and deserialization of PHP variables into MongoDB
51
- ---------------------------------------------------------------
52
-
53
51
``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.
55
64
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::
60
66
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.
64
73
65
74
Consider the following class definition:
66
75
@@ -78,12 +87,9 @@ Consider the following class definition:
78
87
{
79
88
$this->id = new MongoDB\BSON\ObjectID;
80
89
$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;
85
91
}
86
-
92
+
87
93
function bsonSerialize()
88
94
{
89
95
return [
@@ -92,7 +98,7 @@ Consider the following class definition:
92
98
'createdAt' => $this->createdAt,
93
99
];
94
100
}
95
-
101
+
96
102
function bsonUnserialize(array $data)
97
103
{
98
104
$this->id = $data['_id'];
@@ -101,7 +107,7 @@ Consider the following class definition:
101
107
}
102
108
}
103
109
104
- The following constructs a ``Person`` object, inserts it into the
110
+ The following example constructs a ``Person`` object, inserts it into the
105
111
database, and reads it back as an object of the same type:
106
112
107
113
.. code-block:: php
@@ -149,51 +155,47 @@ The same document in the MongoDB shell might display as:
149
155
.. note::
150
156
151
157
: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 .
153
159
154
160
Emulating the Legacy Driver
155
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
161
+ ---------------------------
156
162
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:
160
166
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.
164
170
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.
169
174
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:
176
181
177
182
.. code-block:: php
178
183
179
184
<?php
180
185
181
186
$client = new MongoDB\Client(
182
- null ,
187
+ 'mongodb://127.0.0.1/' ,
183
188
[],
184
- ['typeMap' => [
185
- 'root' => 'array', 'document' => 'array', 'array' => 'array'
189
+ [
190
+ 'typeMap' => [
191
+ 'array' => 'array',
192
+ 'document' => 'array',
193
+ 'root' => 'array',
186
194
],
187
195
]
188
196
);
189
197
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']);
197
199
198
200
var_dump($document);
199
201
0 commit comments