@@ -15,17 +15,17 @@ Overview
1515--------
1616
1717In 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+}.
1919
2020**BSON**, or Binary JSON, is the data format that MongoDB uses to organize
2121and 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.
2525
2626.. tip::
2727
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
2929 </reference/bson-types>` in the {+mdb-server+} manual.
3030
3131.. _php-bson-sample:
@@ -44,13 +44,13 @@ The code examples in this guide reference the following sample BSON document:
4444 },
4545 "coord" : [-73.982419, 41.579505]
4646 "cuisine" : "Pizza",
47- "name" : "Mongo's Pizza"
47+ "name" : "Planet Pizza"
4848 }
4949
5050Create a BSON Document
5151----------------------
5252
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
5454associative array in {+language+}. The {+library-short+} automatically converts
5555these values into BSON documents when inserting them into a collection.
5656
@@ -66,41 +66,48 @@ The following example creates a BSON document that represents the preceding
6666Change a BSON Document
6767----------------------
6868
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
7070modify an associative array in {+language+}. This example makes the following changes
7171to the :ref:`sample BSON document <php-bson-sample>`:
7272
7373- 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"``
7575
7676.. literalinclude:: /includes/bson.php
7777 :language: php
7878 :dedent:
7979 :start-after: start-modify-doc
8080 :end-before: end-modify-doc
8181
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+
8289Customize BSON Serialization
8390----------------------------
8491
8592The following sections describe how to configure your application's
8693serialization of BSON data:
8794
8895- :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.
9097
9198- :ref:`php-persistable-classes`: Use the ``MongoDB\BSON\Persistable`` interface to
92- enable serialization
99+ enable serialization.
93100
94101- :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.
96103
97104.. _php-type-map:
98105
99106Type Maps
100107~~~~~~~~~
101108
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:
104111
105112- ``MongoDB\Client``, which sets the *default for all operations* unless overridden
106113- ``MongoDB\Database``
@@ -120,14 +127,14 @@ The {+library-short+} uses the following type map by default:
120127 'root' => 'MongoDB\Model\BSONDocument',
121128 ]
122129
123- This type map performs the following conversions:
130+ This type map performs the following conversions in both directions :
124131
125132- Arrays to ``MongoDB\Model\BSONArray`` objects
126133- Top-level and embedded BSON documents to ``MongoDB\Model\BSONDocument`` objects
127134
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.
131138
132139Custom Type Map Example
133140```````````````````````
@@ -146,25 +153,22 @@ that serializes arrays and BSON documents as ``MongoDB\Model\BSONDocument`` obje
146153Persistable Classes
147154~~~~~~~~~~~~~~~~~~~
148155
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>`.
154161
155162When deserializing a PHP variable from BSON, the encoded class name of a
156163``Persistable`` object overrides any class specified in the ``typeMap`` option.
157164However, it does not override ``array``, ``stdClass``, or ``object`` types.
158165
159- .. tip::
160-
161- To learn more about the ``Persistable`` interface, see :php:`Persistable <mongodb-bson-persistable>`
162- in the {+extension-short+} documentation.
163-
164166Example
165167```````
166168
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:
168172
169173.. literalinclude:: /includes/bson.php
170174 :language: php
@@ -213,6 +217,10 @@ The returned document is equivalent to the following BSON document:
213217 "createdAt" : ISODate("2016-03-29T19:08:51.218Z")
214218 }
215219
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+
216224.. note::
217225
218226 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
227235enum values serialize as their case value, while pure enums without
228236case values cannot be directly serialized. To perform these conversions,
229237you must specify serialization logic by defining the ``bsonSerialize()``
230- and ``bsonUnserialize()`` methods.
238+ and ``bsonUnserialize()`` methods in your class definition .
231239
232240.. tip::
233241
@@ -243,14 +251,15 @@ methods:
243251
244252.. literalinclude:: /includes/bson.php
245253 :language: php
254+ :emphasize-lines: 15, 24
246255 :dedent:
247256 :start-after: start-backed-enum
248257 :end-before: end-backed-enum
249258
250259.. note::
251260
252261 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
254263 state and cannot be instantiated like class objects. However, pure and backed
255264 enums can implement ``MongoDB\BSON\Serializable``, which you can use to
256265 override the default enum serialization behavior.
0 commit comments