@@ -24,87 +24,119 @@ In this guide, you can learn how to serialize **globally unique identifiers**
2424(`GUIDs <https://learn.microsoft.com/en-us/dynamicsax-2012/developer/guids>`__),
2525also known as **universally unique identifiers** (UUIDs).
2626
27- .. tip::
27+ .. tip:: ObjectId
2828
29- In MongoDB applications, ``ObjectId`` can be used as a unique identifier for
30- a document. Consider using ``ObjectId`` in place of a GUID with MongoDB
31- applications where possible.
29+ In MongoDB applications, you can use the
30+ `ObjectId <{+new-api-root+}/api/MongoDB.Bson/MongoDB.Bson.ObjectId.html>`__ type
31+ as a unique identifier for a document. Consider using ``ObjectId`` instances in place
32+ of GUIDs in MongoDB applications where possible.
3233
33- A GUID is a 16-byte integer that you can use as a unique ID for a MongoDB document.
34- Originally, GUIDs in MongoDB were represented as ``BsonBinaryData`` values of subtype 3.
35- Subtype 3 did not standardize the byte order during serialization, which led to
36- inconsistent serialization across MongoDB drivers.
37- To standardize the byte order and ensure consistent serialization across drivers, we
38- created ``BsonBinaryData`` subtype 4.
34+ GUIDs in MongoDB
35+ ----------------
3936
40- .. note::
41-
42- Use ``BsonBinaryData`` subtype 4 for all new GUIDs.
37+ A GUID is a 16-byte integer that you can use as a unique ID for a MongoDB document.
38+ The following code block shows an example GUID:
4339
44- GuidRepresentationMode
45- ----------------------
40+ .. code-block::
41+ :copyable: false
4642
47- In many MongoDB collections, all GUID fields use the same subtype of ``BsonBinaryData``.
48- Some older collections, however, may contain some GUID fields that
49- use subtype 3 and others that use subtype 4.
50- To ensure that the driver serializes and deserializes all GUIDs correctly,
51- you should set the ``BsonDefaults.GuidRepresentationMode`` property to one of the
52- following ``GuidRepresentationMode`` values:
43+ 00112233-4455-6677-8899-aabbccddeeff
5344
54- V2
55- ~~
45+ Originally, MongoDB represented GUIDs as ``BsonBinaryData``
46+ values of :manual:`subtype 3. </reference/bson-types/#binary-data>`
47+ Because subtype 3 didn't standardize the byte order of GUIDs
48+ during encoding, different MongoDB drivers encoded GUIDs with different byte orders.
5649
57- ``GuidRepresentationMode.V2`` assumes that all GUIDs in a document use the same
58- ``BsonBinaryData`` subtype. In this mode, GUID representation is
59- controlled by the reader or writer, not the serializer.
50+ The following tabs show different driver encodings of the preceding GUID to
51+ ``BsonBinaryData`` subtype 3:
6052
61- ``V2`` is the default ``GuidRepresentationMode``.
53+ .. tabs::
6254
63- .. note::
64-
65- When version 3 of the {+driver-short+} is released, support for ``GuidRepresentationMode.V2``
66- will be removed from the driver and ``V3`` will become the default.
55+ .. tab:: {+driver-short+}
56+ :tabid: csharp
6757
68- V3
69- ~~
58+ .. code-block:: csharp
59+ :copyable: false
7060
71- ``GuidRepresentationMode.V3`` allows fields in the same document to use different
72- GUID formats.
73- In this mode, GUID representation is controlled at the property level by configuring the
74- serializer for each property.
61+ 33221100-5544-7766-8899-aabbccddeeff
7562
76- To use ``GuidRepresentationMode.V3``, run the following line of code. You should run this
77- code during the bootstrapping phase of your application, before creating
78- a ``MongoClient`` object.
63+ .. tab:: PyMongo
64+ :tabid: pymongo
7965
80- .. code-block:: csharp
66+ .. code-block:: python
67+ :copyable: false
68+
69+ 00112233-4455-6677-8899-aabbccddeeff
70+
71+ .. tab:: Java Driver
72+ :tabid: java
73+
74+ .. code-block:: java
75+ :copyable: false
76+
77+ 77665544-3322-1100-ffee-ddccbbaa9988
78+
79+ To standardize GUID byte order across applications, we added ``BsonBinaryData`` subtype 4,
80+ which all MongoDB drivers encode in the same way. If your application uses GUIDs, we
81+ recommend using ``BsonBinaryData`` subtype 4 to store them.
82+
83+ For a list of all ``BsonBinaryData`` subtypes, see the
84+ API documentation for the `BsonBinarySubType <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonBinarySubType.html>`__
85+ enum.
8186
82- BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
87+ Serializing GUIDs
88+ -----------------
8389
84- Running in ``V3`` mode changes the behavior of the driver in the following ways:
90+ Although we recommend using subtype 4 for all new ``BsonBinaryData`` GUIDs, some older
91+ MongoDB collections might contain some GUID fields that use subtype 3 and others that use
92+ subtype 4. To account for these differences, the {+driver-short+} handles GUID
93+ serialization at the level of individual properties.
8594
86- - The ``BsonBinaryReader.ReadBinaryData()`` method ignores ``readerSettings.GuidRepresentation``
87- - The ``BsonBinaryWriter.WriteBinaryData()`` method ignores ``writerSettings.GuidRepresentation``
88- - The ``JsonReader.ReadBinaryData()`` method ignores ``readerSettings.GuidRepresentation``
89- - ``JsonWriter`` ignores ``writerSettings.GuidRepresentation``
90- - Calling the ``BsonBinaryData.ToGuid()`` method without the ``GuidRepresentation``
91- parameter works only on GUIDs of subtype 4.
95+ The {+driver-short+} uses the ``GuidRepresentation`` enum to represent the different
96+ ``BsonBinaryData`` subtypes. The following table shows the ``GuidRepresentation`` enum
97+ members and the corresponding ``BsonBinaryData`` subtypes:
98+
99+ .. list-table::
100+ :header-rows: 1
101+ :stub-columns: 1
102+ :widths: 10 10
103+
104+ * - GuidRepresentation Member
105+ - BsonBinaryData Subtype
106+
107+ * - ``Standard``
108+ - 4
109+
110+ * - ``CSharpLegacy``
111+ - 3
112+
113+ * - ``JavaLegacy``
114+ - 3
115+
116+ * - ``PythonLegacy``
117+ - 3
118+
119+ * - ``Unspecified``
120+ - N/A
92121
93122.. note::
123+
124+ The ``CSharpLegacy``, ``JavaLegacy``, and ``PythonLegacy`` GUID representations are
125+ all equivalent to ``BsonBinaryData`` subtype 3, but use different byte orders.
94126
95- You can't use both ``GuidRepresentationMode.V2`` and ``GuidRepresentationMode.V3``
96- in a single application.
127+ The following sections describe the ways in which you can configure GUID representation
128+ in your application.
97129
98- Serializing GUIDs in V3
99- -----------------------
130+ Configure with Attributes
131+ ~~~~~~~~~~~~~~~~~~~~~~~~~
100132
101- ``GuidRepresentationMode.V3`` handles GUID serialization at the level of individual
102- properties. This mode is more flexible than ``V2``, but it also means you must ensure that
103- each GUID field is serialized and deserialized correctly.
133+ If you're using the {+driver-short+} to
134+ :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
135+ you can add the ``BsonGuidRepresentation`` attribute to a GUID property
136+ to specify its representation. This attribute accepts a value from the
104137
105- If you're using the {+driver-short+} to :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
106- you can use the ``BsonGuidRepresentation`` attribute on a GUID property
107- to specify the representation:
138+ The following code example specifies the ``Standard`` GUID representation for the
139+ ``G`` property:
108140
109141.. code-block:: csharp
110142
@@ -116,20 +148,16 @@ to specify the representation:
116148 public Guid G { get; set; }
117149 }
118150
119- .. note::
120-
121- ``GuidRepresentation.Standard`` is equivalent to ``BsonBinaryData`` subtype 4.
122- Other GUID representations in the {+driver-short+}, such as ``CSharpLegacy``,
123- ``JavaLegacy``, and ``PythonLegacy``, are equivalent to subtype 3 but use
124- different byte orders.
151+ Configure in Code
152+ ~~~~~~~~~~~~~~~~~
125153
126154If you're writing your own serialization code, you can use the
127155``GuidSerializer`` class to serialize and deserialize individual GUID values to and
128156from BSON fields. To ensure that the driver handles GUIDs correctly, use the
129157``GuidRepresentation`` parameter when you construct a ``GuidSerializer``.
130158
131- The following code sample creates an instance of ``GuidSerializer``
132- for serializing GUID representations of subtype 4:
159+ The following code sample creates an instance of the ``GuidSerializer`` class
160+ for serializing properties that use ``BsonBinaryData`` subtype 4:
133161
134162.. code-block::
135163
@@ -145,18 +173,23 @@ in your application, such as during the bootstrapping phase:
145173
146174.. tip::
147175
148- When you're working with two subtypes, you can combine a global serializer with the
149- ``BsonGuidRepresentation`` property attribute. For example, you can register a global
150- serializer for the most commonly used GUID subtype, then use the ``BsonGuidRepresentation``
151- attribute to denote any GUID properties of another subtype.
176+ When you're working with two ``BsonBinaryData`` subtypes, you can combine a global
177+ serializer with the ``BsonGuidRepresentation`` property attribute. For example, you
178+ can register a global serializer for the most commonly used GUID subtype, then use
179+ the ``BsonGuidRepresentation`` attribute to denote any GUID properties of another subtype.
152180
153- Serializing Objects in V3
154- -------------------------
181+ .. important::
182+
183+ If you don't globally register a serializer, you must apply the ``BsonGuidRepresentation``
184+ attribute to every serializable GUID property. Otherwise, the driver throws an exception
185+ when it tries to serialize the property.
186+
187+ Serializing Objects
188+ -------------------
155189
156190You can use an ``ObjectSerializer`` to serialize hierarchical objects to subdocuments.
157- To ensure that GUIDs in these objects are serialized and deserialized correctly when using
158- ``V3``, you should select the correct GUID representation when constructing your
159- ``ObjectSerializer``.
191+ To ensure that GUIDs in these objects are serialized and deserialized correctly,
192+ select the correct GUID representation when constructing your ``ObjectSerializer``.
160193
161194The following code sample shows how to
162195create an ``ObjectSerializer`` for a GUID representation of subtype 4:
@@ -192,5 +225,4 @@ guide, see the following API documentation:
192225- `GuidRepresentationMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.GuidRepresentationMode.html>`__
193226- `BsonGuidRepresentation <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Attributes.BsonGuidRepresentationAttribute.html>`__
194227- `GuidSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.GuidSerializer.html>`__
195- - `ObjectSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.ObjectSerializer.html>`__
196-
228+ - `ObjectSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.ObjectSerializer.html>`__
0 commit comments