@@ -24,47 +24,104 @@ In this guide, you can learn how to serialize **globally unique identifiers**
24
24
(`GUIDs <https://learn.microsoft.com/en-us/dynamicsax-2012/developer/guids>`__),
25
25
also known as **universally unique identifiers** (UUIDs).
26
26
27
- .. tip::
27
+ A Short History of MongoDB GUIDs
28
+ --------------------------------
28
29
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.
30
+ A GUID is a 16-byte integer that you can use as a unique ID for a MongoDB document.
31
+ The following code block shows an example GUID:
32
+
33
+ .. code-block::
32
34
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.
35
+ 00112233-4455-6677-8899-aabbccddeeff
39
36
40
- .. note::
41
-
42
- Use ``BsonBinaryData`` subtype 4 for all new GUIDs.
37
+ Originally, MongoDB represented GUIDs as ``BsonBinaryData``
38
+ values of subtype 3. Because subtype 3 didn't standardize the byte order of GUIDs
39
+ during encoding, different MongoDB drivers encoded GUIDs with different byte orders.
40
+
41
+ Use the following tabs to compare the ways in which different MongoDB language drivers
42
+ encoded the preceding GUID to ``BsonBinaryData`` subtype 3:
43
+
44
+ .. tabs::
43
45
46
+ .. tab:: {+driver-short+}
47
+ :tabid: csharp
44
48
45
- In many MongoDB collections, all GUID fields use the same subtype of ``BsonBinaryData``.
46
- Some older collections, however, may contain some GUID fields that
47
- use subtype 3 and others that use subtype 4.
49
+ .. code-block:: csharp
48
50
49
- The driver allows fields in the same document to use different
50
- GUID formats. GUID representation is controlled at the property level by configuring the
51
- serializer for each property.
51
+ 33221100-5544-7766-8899-aabbccddeeff
52
+
53
+ .. tab:: PyMongo
54
+ :tabid: pymongo
55
+
56
+ .. code-block:: python
57
+
58
+ 00112233-4455-6677-8899-aabbccddeeff
59
+
60
+ .. tab:: Java Driver
61
+ :tabid: java
62
+
63
+ .. code-block:: java
64
+
65
+ 77665544-3322-1100-ffee-ddccbbaa9988
66
+
67
+ To standardize GUID byte order, we added ``BsonBinaryData`` subtype 4, which all
68
+ MongoDB drivers encode in the same way. We recommend using
69
+ ``BsonBinaryData`` subtype 4 for all new GUIDs.
70
+
71
+ For a list of all ``BsonBinaryData`` subtypes, see the
72
+ API documentation for the `BsonBinarySubType <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonBinarySubType.html>`__
73
+ enum.
74
+
75
+ .. tip::
76
+
77
+ In MongoDB applications, ``ObjectId`` can be used as a unique identifier for
78
+ a document. Consider using ``ObjectId`` in place of a GUID with MongoDB
79
+ applications where possible.
52
80
53
81
Serializing GUIDs
54
82
-----------------
55
83
56
- The driver handles GUID serialization at the level of individual
57
- properties. You must ensure that
58
- each GUID field is serialized and deserialized correctly.
84
+ Although we recommend using subtype 4 for all new ``BsonBinaryData`` GUIDs, some older
85
+ MongoDB collections might contain some GUID fields that use subtype 3 and others that use
86
+ subtype 4. To account for these differences, the {+driver-short+} handles GUID
87
+ serialization at the level of individual properties.
59
88
60
- .. note::
89
+ If you're using the {+driver-short+} to
90
+ :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
91
+ you can add the ``BsonGuidRepresentation`` attribute to a GUID property
92
+ to specify its representation. This attribute accepts a value from the
93
+ ``GuidRepresentation`` enum, which contains the following members:
61
94
62
- Default value is unspecified. Driver throws an exception rather than guessing.
63
- You must specify the format for every GUID, or globally register a serializer.
95
+ .. list-table::
96
+ :header-rows: 1
97
+ :stub-columns: 1
98
+ :widths: 10 10
64
99
65
- If you're using the {+driver-short+} to :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
66
- you can use the ``BsonGuidRepresentation`` attribute on a GUID property
67
- to specify the representation:
100
+ * - GuidRepresentation Member
101
+ - BsonBinaryData Subtype
102
+
103
+ * - ``Standard``
104
+ - 4
105
+
106
+ * - ``CSharpLegacy``
107
+ - 3
108
+
109
+ * - ``JavaLegacy``
110
+ - 3
111
+
112
+ * - ``PythonLegacy``
113
+ - 3
114
+
115
+ * - ``Unspecified``
116
+ - N/A
117
+
118
+ .. note::
119
+
120
+ The ``CSharpLegacy``, ``JavaLegacy``, and ``PythonLegacy`` GUID representations are
121
+ all equivalent to ``BsonBinaryData`` subtype 3, but use different byte orders.
122
+
123
+ The following code example specifies the ``Standard`` GUID representation for the
124
+ ``G`` property:
68
125
69
126
.. code-block:: csharp
70
127
@@ -76,20 +133,13 @@ to specify the representation:
76
133
public Guid G { get; set; }
77
134
}
78
135
79
- .. note::
80
-
81
- ``GuidRepresentation.Standard`` is equivalent to ``BsonBinaryData`` subtype 4.
82
- Other GUID representations in the {+driver-short+}, such as ``CSharpLegacy``,
83
- ``JavaLegacy``, and ``PythonLegacy``, are equivalent to subtype 3 but use
84
- different byte orders.
85
-
86
136
If you're writing your own serialization code, you can use the
87
137
``GuidSerializer`` class to serialize and deserialize individual GUID values to and
88
138
from BSON fields. To ensure that the driver handles GUIDs correctly, use the
89
139
``GuidRepresentation`` parameter when you construct a ``GuidSerializer``.
90
140
91
- The following code sample creates an instance of ``GuidSerializer``
92
- for serializing GUID representations of subtype 4:
141
+ The following code sample creates an instance of the ``GuidSerializer`` class
142
+ for serializing properties that use ``BsonBinaryData`` subtype 4:
93
143
94
144
.. code-block::
95
145
@@ -110,6 +160,12 @@ in your application, such as during the bootstrapping phase:
110
160
serializer for the most commonly used GUID subtype, then use the ``BsonGuidRepresentation``
111
161
attribute to denote any GUID properties of another subtype.
112
162
163
+ .. important::
164
+
165
+ If you don't globally register a serializer, you must apply the ``BsonGuidRepresentation``
166
+ attribute to every serializable GUID property. Otherwise, the driver throws an exception
167
+ when it tries to serialize the property.
168
+
113
169
Serializing Objects
114
170
-------------------
115
171
0 commit comments