Skip to content

Commit cda9e05

Browse files
committed
feedback
1 parent b2e682d commit cda9e05

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

source/fundamentals/serialization/guid-serialization.txt

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ In this guide, you can learn how to serialize **globally unique identifiers**
2424
(`GUIDs <https://learn.microsoft.com/en-us/dynamicsax-2012/developer/guids>`__),
2525
also known as **universally unique identifiers** (UUIDs).
2626

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+
----------------
2936

3037
A GUID is a 16-byte integer that you can use as a unique ID for a MongoDB document.
3138
The following code block shows an example GUID:
3239

3340
.. code-block::
41+
:copyable: false
3442

3543
00112233-4455-6677-8899-aabbccddeeff
3644

@@ -47,37 +55,34 @@ encoded the preceding GUID to ``BsonBinaryData`` subtype 3:
4755
:tabid: csharp
4856

4957
.. code-block:: csharp
58+
:copyable: false
5059

5160
33221100-5544-7766-8899-aabbccddeeff
5261

5362
.. tab:: PyMongo
5463
:tabid: pymongo
5564

5665
.. code-block:: python
66+
:copyable: false
5767

5868
00112233-4455-6677-8899-aabbccddeeff
5969

6070
.. tab:: Java Driver
6171
:tabid: java
6272

6373
.. code-block:: java
74+
:copyable: false
6475

6576
77665544-3322-1100-ffee-ddccbbaa9988
6677

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.
7081

7182
For a list of all ``BsonBinaryData`` subtypes, see the
7283
API documentation for the `BsonBinarySubType <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BsonBinarySubType.html>`__
7384
enum.
7485

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-
8186
Serializing GUIDs
8287
-----------------
8388

@@ -86,11 +91,9 @@ MongoDB collections might contain some GUID fields that use subtype 3 and others
8691
subtype 4. To account for these differences, the {+driver-short+} handles GUID
8792
serialization at the level of individual properties.
8893

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:
9497

9598
.. list-table::
9699
:header-rows: 1
@@ -120,6 +123,17 @@ to specify its representation. This attribute accepts a value from the
120123
The ``CSharpLegacy``, ``JavaLegacy``, and ``PythonLegacy`` GUID representations are
121124
all equivalent to ``BsonBinaryData`` subtype 3, but use different byte orders.
122125

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+
123137
The following code example specifies the ``Standard`` GUID representation for the
124138
``G`` property:
125139

@@ -133,6 +147,9 @@ The following code example specifies the ``Standard`` GUID representation for th
133147
public Guid G { get; set; }
134148
}
135149

150+
Configure in Code
151+
~~~~~~~~~~~~~~~~~
152+
136153
If you're writing your own serialization code, you can use the
137154
``GuidSerializer`` class to serialize and deserialize individual GUID values to and
138155
from BSON fields. To ensure that the driver handles GUIDs correctly, use the
@@ -171,8 +188,7 @@ Serializing Objects
171188

172189
You can use an ``ObjectSerializer`` to serialize hierarchical objects to subdocuments.
173190
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``.
176192

177193
The following code sample shows how to
178194
create an ``ObjectSerializer`` for a GUID representation of subtype 4:
@@ -208,5 +224,4 @@ guide, see the following API documentation:
208224
- `GuidRepresentationMode <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.GuidRepresentationMode.html>`__
209225
- `BsonGuidRepresentation <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.Serialization.Attributes.BsonGuidRepresentationAttribute.html>`__
210226
- `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>`__

source/upgrade.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ Version 3.0 Potential Breaking Change
7676
In version 3.0, ``GuidRepresentationMode.V3`` is the only supported mode. This change
7777
has the following effects on the driver:
7878

79-
- The methods in the ``BsonBinaryReader``, ``BsonBinaryWriter``, ``JsonReader``, and
80-
``JsonWriter`` classes no longer check the ``GuidRepresentationMode``.
8179
- The ``BsonBinaryData(Guid)`` constructor has been removed. To construct a ``BsonBinaryData``
8280
object from a GUID, use the ``BsonBinaryData.Create(Guid, GuidRepresentation)`` constructor.
8381
- The ``BsonBinaryData.GuidRepresentation`` property has been removed.
@@ -89,6 +87,9 @@ Version 3.0 Potential Breaking Change
8987
BSON documents directly. If you map your MongoDB documents only to :ref:`csharp-poco`,
9088
the ``GuidRepresentationMode`` doesn't affect your application.
9189

90+
To learn more about serializing GUIDs in the {+driver-short+}, see the
91+
:ref:`GUIDs <csharp-guids>` page.
92+
9293
.. _csharp-breaking-changes-2.28.0:
9394

9495
Version 2.28.0 Potential Breaking Change

0 commit comments

Comments
 (0)