@@ -15,17 +15,17 @@ Overview
15
15
--------
16
16
17
17
In this guide, you can learn how to create and interact with BSON documents
18
- by using the {+driver -short+}.
18
+ by using the {+library -short+}.
19
19
20
20
**BSON**, or Binary JSON, is the data format that MongoDB uses to organize
21
21
and store data. This data format includes all JSON data structure types and
22
- supports additional types, including dates, different-sized integers, ``ObjectId``
23
- values, and binary data. The {+library-short+} provides the `` MongoDB\Model\BSONArray` `
24
- and `` MongoDB\Model\BSONDocument` ` types to store BSON data.
22
+ also supports other types, including dates, different-sized integers, ``ObjectId``
23
+ values, and binary data. The {+library-short+} provides the :phpclass:` MongoDB\Model\BSONArray`
24
+ and :phpclass:` MongoDB\Model\BSONDocument` types to store BSON data.
25
25
26
26
.. tip::
27
27
28
- For a complete list of supported BSON types, see :manual:`BSON Types
28
+ To view a complete list of supported BSON types, see :manual:`BSON Types
29
29
</reference/bson-types>` in the {+mdb-server+} manual.
30
30
31
31
.. _php-bson-sample:
@@ -44,13 +44,13 @@ The code examples in this guide reference the following sample BSON document:
44
44
},
45
45
"coord" : [-73.982419, 41.579505]
46
46
"cuisine" : "Pizza",
47
- "name" : "Mongo's Pizza"
47
+ "name" : "Planet Pizza"
48
48
}
49
49
50
50
Create a BSON Document
51
51
----------------------
52
52
53
- You can create a BSON document by using the same notation you use to create an
53
+ You can create a BSON document by using the same notation that you use to create an
54
54
associative array in {+language+}. The {+library-short+} automatically converts
55
55
these values into BSON documents when inserting them into a collection.
56
56
@@ -66,41 +66,48 @@ The following example creates a BSON document that represents the preceding
66
66
Change a BSON Document
67
67
----------------------
68
68
69
- You can modify the contents of a BSON document by using the same notation you use to
69
+ You can modify the contents of a BSON document by using the same notation that you use to
70
70
modify an associative array in {+language+}. This example makes the following changes
71
71
to the :ref:`sample BSON document <php-bson-sample>`:
72
72
73
73
- Adds a new ``restaurant_id`` field that has a value of ``12345``
74
- - Sets the value of the ``name`` field to ``"Mongo's Pizza Place "``
74
+ - Changes the ``name`` field value to ``"Galaxy Pizza"``
75
75
76
76
.. literalinclude:: /includes/bson.php
77
77
:language: php
78
78
:dedent:
79
79
:start-after: start-modify-doc
80
80
:end-before: end-modify-doc
81
81
82
+ .. note::
83
+
84
+ The preceding code changes only the in-memory values of the sample BSON
85
+ document. It does not run any database operations that change values stored
86
+ in MongoDB. To learn how to modify documents stored in MongoDB, see the
87
+ :ref:`php-write-update` guide.
88
+
82
89
Customize BSON Serialization
83
90
----------------------------
84
91
85
92
The following sections describe how to configure your application's
86
93
serialization of BSON data:
87
94
88
95
- :ref:`php-type-map`: Use the ``typeMap`` option to specify the default conversion
89
- between {+language+} types and BSON types
96
+ between {+language+} types and BSON types.
90
97
91
98
- :ref:`php-persistable-classes`: Use the ``MongoDB\BSON\Persistable`` interface to
92
- enable serialization
99
+ enable serialization.
93
100
94
101
- :ref:`php-enums`: Use the ``bsonSerialize()`` and ``bsonUnserialize()`` methods to
95
- specify serialization between backed enums and BSON values
102
+ specify serialization between backed enums and BSON values.
96
103
97
104
.. _php-type-map:
98
105
99
106
Type Maps
100
107
~~~~~~~~~
101
108
102
- You can set the ``typeMap`` option, which configures how BSON values are converted to
103
- {+language+} values, at the following levels:
109
+ You can set the ``typeMap`` option, which configures serialization and
110
+ deserialization between {+language+} and BSON values, at the following levels:
104
111
105
112
- ``MongoDB\Client``, which sets the *default for all operations* unless overridden
106
113
- ``MongoDB\Database``
@@ -120,14 +127,14 @@ The {+library-short+} uses the following type map by default:
120
127
'root' => 'MongoDB\Model\BSONDocument',
121
128
]
122
129
123
- This type map performs the following conversions:
130
+ This type map performs the following conversions in both directions :
124
131
125
132
- Arrays to ``MongoDB\Model\BSONArray`` objects
126
133
- Top-level and embedded BSON documents to ``MongoDB\Model\BSONDocument`` objects
127
134
128
- A type map can specify any class that implements
129
- :php:`MongoDB\BSON\Unserializable <mongodb-bson-unserializable>`. It can
130
- also specify the ``array``, ``stdClass``, and ``object`` types.
135
+ A type map can specify any class that implements the
136
+ :php:`MongoDB\BSON\Unserializable <mongodb-bson-unserializable>` interface.
137
+ It can also specify conversions of the ``array``, ``stdClass``, and ``object`` types.
131
138
132
139
Custom Type Map Example
133
140
```````````````````````
@@ -146,25 +153,22 @@ that serializes arrays and BSON documents as ``MongoDB\Model\BSONDocument`` obje
146
153
Persistable Classes
147
154
~~~~~~~~~~~~~~~~~~~
148
155
149
- You can create classes that implement the `` MongoDB\BSON\Persistable`` interface,
150
- which instructs the {+library-short+} to automatically perform serialization and
151
- deserialization according to the {+extension-short+}'s :php:`persistence specification
152
- <mongodb.persistence>`. The ``Persistable`` interface is analogous to {+language+}'s
153
- :php:`Serializable interface <class.serializable>`.
156
+ You can create classes that implement the :php:` MongoDB\BSON\Persistable <mongodb-bson-persistable>`
157
+ interface. This interface instructs the {+library-short+} to automatically perform serialization
158
+ and deserialization according to the {+extension-short+}'s :php:`persistence specification
159
+ <mongodb.persistence>` without requiring the ``typeMap`` option . The ``Persistable`` interface
160
+ is analogous to {+language+}'s :php:`Serializable interface <class.serializable>`.
154
161
155
162
When deserializing a PHP variable from BSON, the encoded class name of a
156
163
``Persistable`` object overrides any class specified in the ``typeMap`` option.
157
164
However, it does not override ``array``, ``stdClass``, or ``object`` types.
158
165
159
- .. tip::
160
-
161
- To learn more about the ``Persistable`` interface, see :php:`Persistable <mongodb-bson-persistable>`
162
- in the {+extension-short+} documentation.
163
-
164
166
Example
165
167
```````
166
168
167
- Consider the following ``Person`` class definition:
169
+ Consider the following ``Person`` class definition, which implements the
170
+ ``Persistable`` interface and specifies how to serialize and deserialize
171
+ object fields as BSON values:
168
172
169
173
.. literalinclude:: /includes/bson.php
170
174
:language: php
@@ -213,6 +217,10 @@ The returned document is equivalent to the following BSON document:
213
217
"createdAt" : ISODate("2016-03-29T19:08:51.218Z")
214
218
}
215
219
220
+ The {+library-short+} automatically adds the ``__pclass`` field to keep
221
+ track of the document's corresponding class name, which allows you to
222
+ deserialize the document into a ``Person`` object.
223
+
216
224
.. note::
217
225
218
226
You can use the ``Persistable`` interface for root and embedded BSON documents
@@ -227,7 +235,7 @@ You can serialize and deserialize backed enums into BSON data. Backed
227
235
enum values serialize as their case value, while pure enums without
228
236
case values cannot be directly serialized. To perform these conversions,
229
237
you must specify serialization logic by defining the ``bsonSerialize()``
230
- and ``bsonUnserialize()`` methods.
238
+ and ``bsonUnserialize()`` methods in your class definition .
231
239
232
240
.. tip::
233
241
@@ -243,14 +251,15 @@ methods:
243
251
244
252
.. literalinclude:: /includes/bson.php
245
253
:language: php
254
+ :emphasize-lines: 15, 24
246
255
:dedent:
247
256
:start-after: start-backed-enum
248
257
:end-before: end-backed-enum
249
258
250
259
.. note::
251
260
252
261
Enums cannot implement the ``MongoDB\BSON\Unserializable`` and
253
- ``MongoDB\BSON\Persistable`` interfaces, since enum cases have no
262
+ ``MongoDB\BSON\Persistable`` interfaces because enum cases have no
254
263
state and cannot be instantiated like class objects. However, pure and backed
255
264
enums can implement ``MongoDB\BSON\Serializable``, which you can use to
256
265
override the default enum serialization behavior.
0 commit comments