C# GUID Style dont work

hello
i want use guid standart style for C# without Binary(32434), HexData(34342).

In the past I have used:

BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;

but now is deprecated

i find new :

BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;

but its deprecated too…

i find in manual that need use “GuidSerializer changes”

https://mongodb.github.io/mongo-csharp-driver/2.13/reference/bson/guidserialization/serializerchanges/guidserializerchanges/

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

and i get again HexData :
image

what should i do to make it work like before?

Hi, Alexov,

GuidRepresentation.Standard is our recommended approach, which stores and transmits GUIDs using BsonBinaryData subtype 4. Separate but related is the GuidRepresentationMode, which is how the GuidRepresentation is configured. In V2 mode, GuidRepresentation is configured at the collection level where as V3 mode it is configured at the individual property level.

When using V3 mode, you can configure the default GuidSerializer globally as you noted using:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

Due to backward compatibility concerns, we were unable to change these defaults to GuidRepresentation.Standard, GuidRepresentationMode.V3, and the default GUID serializer in the 2.X driver series. BsonDefaults.GuidRepresentation and BsonDefaults.GuidRepresentationMode allow you to opt into these preferred defaults.

Now for the confusing aspect… We intend to make GuidRepresentation.Standard and GuidRepresentationMode.V3 the defaults in a future version of the driver and eventually not support the old defaults. Thus BsonDefaults.GuidRepresentation and BsonDefaults.GuidRepresentationMode will be going away in a future version of the driver as these options will not be configurable, which is why we marked these APIs as deprecated. However we marked them as deprecated too aggressively as we fully intend users to use these properties to set the GUID-related defaults in the v2.X series of the driver.

You can disable these warnings with a #pragma as follows as we do internally within the driver code:

#pragma warning disable 618
BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
#pragma warning restore

Please let us know if you have any additional questions.

Sincerely,
James

2 Likes

oh yes its work only if create new collection

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

But if add this line and try add more rows in old collection its dont work.

what do i should do for old data? Create new collection and migrate data ?

Hi, Alexov,

When working with POCOs in the .NET/C# driver, different GUIDs within a single document can use different GUID representations, but all GUIDs for a single field (e.g. _id) must use the same representation. This is because we need to know how to map the array of bytes to fields within the GUID.

If your existing collection doesn’t contain a lot of data, the easiest solution is to create a new collection and migrate the existing data to it ensuring that all GUIDs use subtype 4. If your existing collection contains a substantial amount of data that you want to migrate on modification, you could implement your own IBsonSerializer that can read both subtype 3 and subtype 4 GUIDs but will always write as subtype 4.

Sincerely,
James

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.