@@ -24,13 +24,21 @@ 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
- A Short History of MongoDB GUIDs
28
- --------------------------------
27
+ .. tip:: ObjectId
28
+
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`` in place of a GUID
32
+ in MongoDB applications where possible.
33
+
34
+ GUIDs in MongoDB
35
+ ----------------
29
36
30
37
A GUID is a 16-byte integer that you can use as a unique ID for a MongoDB document.
31
38
The following code block shows an example GUID:
32
39
33
40
.. code-block::
41
+ :copyable: false
34
42
35
43
00112233-4455-6677-8899-aabbccddeeff
36
44
@@ -47,37 +55,34 @@ encoded the preceding GUID to ``BsonBinaryData`` subtype 3:
47
55
:tabid: csharp
48
56
49
57
.. code-block:: csharp
58
+ :copyable: false
50
59
51
60
33221100-5544-7766-8899-aabbccddeeff
52
61
53
62
.. tab:: PyMongo
54
63
:tabid: pymongo
55
64
56
65
.. code-block:: python
66
+ :copyable: false
57
67
58
68
00112233-4455-6677-8899-aabbccddeeff
59
69
60
70
.. tab:: Java Driver
61
71
:tabid: java
62
72
63
73
.. code-block:: java
74
+ :copyable: false
64
75
65
76
77665544-3322-1100-ffee-ddccbbaa9988
66
77
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 .
78
+ To standardize GUID byte order across applications , we added ``BsonBinaryData`` subtype 4,
79
+ which all MongoDB drivers encode in the same way. If your application uses GUIDs, we
80
+ recommend using ``BsonBinaryData`` subtype 4 to store them .
70
81
71
82
For a list of all ``BsonBinaryData`` subtypes, see the
72
83
API documentation for the `BsonBinarySubType <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonBinarySubType.html>`__
73
84
enum.
74
85
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.
80
-
81
86
Serializing GUIDs
82
87
-----------------
83
88
@@ -86,11 +91,9 @@ MongoDB collections might contain some GUID fields that use subtype 3 and others
86
91
subtype 4. To account for these differences, the {+driver-short+} handles GUID
87
92
serialization at the level of individual properties.
88
93
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:
94
+ The {+driver-short+} uses the ``GuidRepresentation`` enum to represent the different
95
+ ``BsonBinaryData`` subtypes. The following table shows the ``GuidRepresentation`` enum
96
+ members and the corresponding ``BsonBinaryData`` subtypes:
94
97
95
98
.. list-table::
96
99
:header-rows: 1
@@ -120,6 +123,17 @@ to specify its representation. This attribute accepts a value from the
120
123
The ``CSharpLegacy``, ``JavaLegacy``, and ``PythonLegacy`` GUID representations are
121
124
all equivalent to ``BsonBinaryData`` subtype 3, but use different byte orders.
122
125
126
+ The following sections describe the ways in which you can configure GUID representation
127
+ in your application.
128
+
129
+ Configure with Attributes
130
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
131
+
132
+ If you're using the {+driver-short+} to
133
+ :ref:`automap your {+language+} classes to document schemas <csharp-class-mapping>`,
134
+ you can add the ``BsonGuidRepresentation`` attribute to a GUID property
135
+ to specify its representation. This attribute accepts a value from the
136
+
123
137
The following code example specifies the ``Standard`` GUID representation for the
124
138
``G`` property:
125
139
@@ -133,6 +147,9 @@ The following code example specifies the ``Standard`` GUID representation for th
133
147
public Guid G { get; set; }
134
148
}
135
149
150
+ Configure in Code
151
+ ~~~~~~~~~~~~~~~~~
152
+
136
153
If you're writing your own serialization code, you can use the
137
154
``GuidSerializer`` class to serialize and deserialize individual GUID values to and
138
155
from BSON fields. To ensure that the driver handles GUIDs correctly, use the
@@ -171,8 +188,7 @@ Serializing Objects
171
188
172
189
You can use an ``ObjectSerializer`` to serialize hierarchical objects to subdocuments.
173
190
To ensure that GUIDs in these objects are serialized and deserialized correctly,
174
- you should select the correct GUID representation when constructing your
175
- ``ObjectSerializer``.
191
+ select the correct GUID representation when constructing your ``ObjectSerializer``.
176
192
177
193
The following code sample shows how to
178
194
create an ``ObjectSerializer`` for a GUID representation of subtype 4:
@@ -208,5 +224,4 @@ guide, see the following API documentation:
208
224
- `GuidRepresentationMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.GuidRepresentationMode.html>`__
209
225
- `BsonGuidRepresentation <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Attributes.BsonGuidRepresentationAttribute.html>`__
210
226
- `GuidSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.GuidSerializer.html>`__
211
- - `ObjectSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.ObjectSerializer.html>`__
212
-
227
+ - `ObjectSerializer <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Serializers.ObjectSerializer.html>`__
0 commit comments