@@ -15,9 +15,10 @@ Codecs
15
15
Overview
16
16
--------
17
17
18
- Codecs are used to decode BSON documents into PHP objects and vice versa. In contrast to other methods, they allow for
19
- more flexibility and customization of the process and how different data types are handled. They allow separating the
20
- logic for BSON encoding and decoding from the domain classes, allowing to decode BSON into plain old PHP objects (POPOs).
18
+ Codecs are used to decode BSON documents into PHP objects, and encode PHP objects into BSON documents. In contrast to
19
+ other methods, they allow for more flexibility and customization of the process and how different data types are
20
+ handled. They allow separating the logic for BSON encoding and decoding from the domain classes, allowing to decode BSON
21
+ into plain old PHP objects (POPOs).
21
22
22
23
Document Codec Usage
23
24
--------------------
@@ -102,14 +103,18 @@ To then use this codec with a collection, specify the ``codec`` option when sele
102
103
103
104
The example above selects a collection and instructs it to use the ``PersonCodec`` for encoding and decoding documents.
104
105
When inserting data, the ``PersonCodec`` is used to encode the document. When retrieving data, the ``PersonCodec`` is
105
- used to decode BSON data into a ``Person`` instance.
106
-
107
- .. note::
108
-
109
- When working on a collection with a codec, all incoming and outgoing data will be handled with the given codec. This
110
- affects the following methods: ``aggregate``, ``bulkWrite``, ``find``, ``findOne``, ``findOneAndDelete``,
111
- ``findOneAndReplace``, ``findOneAndUpdate``, ``insertMany``, ``insertOne``, and ``replaceOne``. To disable the codec
112
- for a specific operation, you can provide ``null`` for the ``codec`` option in the operation.
106
+ used to decode BSON data into a ``Person`` instance. Note that while the ``PersonCodec`` could technically decode any
107
+ BSON document that contains a name field, we wouldn't want to use it for any other documents. Document codecs are meant
108
+ to be used in a collection, or when decoding embedded documents.
109
+
110
+ When working on a collection with a codec, the codec will only accept and return data of that type for certain
111
+ operations. The ``bulkWrite``, ```findOneAndReplace``, ``insertMany``, ``insertOne``, and ``replaceOne`` operations
112
+ will attempt to encode the given data using the provided codec. Trying to insert or replace a document that cannot be
113
+ encoded will result in an exception. The ``aggregate``, ``find``, ``findOne``, ``findOneAndDelete``,
114
+ ``findOneAndReplace``, and ``findOneAndUpdate`` operations will attempt to decode returned documents using the provided
115
+ codec. If the codec does not support the data returned, an exception will be thrown. You can disable codec usage for a
116
+ specific operation or use a different codec (e.g. to decode the result of an aggregation pipeline) by specifying the
117
+ ``codec`` option for the operation. This will override the collection-level codec.
113
118
114
119
Generic Codecs
115
120
--------------
@@ -228,7 +233,7 @@ Codec Libraries
228
233
229
234
If you have a number of codecs that you want to use in multiple places, you can create a codec library. A codec library
230
235
contains a list of codecs and checks each codec if it supports a value. If it does, it will use that codec to encode or
231
- decode the given value. The following code snippet changes the PersonCodec to use a codec library instead of a
236
+ decode the given value. The following code snippet changes the `` PersonCodec`` to use a codec library instead of a
232
237
hard-coded ``DateTimeCodec``:
233
238
234
239
.. code-block:: php
@@ -293,9 +298,9 @@ This way, you can configure a global codec library and reuse it in multiple plac
293
298
Library-aware Codecs
294
299
~~~~~~~~~~~~~~~~~~~~
295
300
296
- If you have a codec that needs to be aware of the codec library, you can implement the ``MongoDB\Codec\LibraryAware``
297
- interface. When the codec is added to a library, this library is then injected into the codec. This allows you to create
298
- a common library for all value codecs and re-use it in multiple document codecs.
301
+ If you have a codec that needs to be aware of the codec library, you can implement the
302
+ ``MongoDB\Codec\KnowsCodecLibrary`` interface. When the codec is added to a library, this library is then injected into
303
+ the codec. This allows you to create a common library for all value codecs and re-use it in multiple document codecs.
299
304
300
305
.. note::
301
306
0 commit comments