Mongodb c# driver 2.11 Guid serialization issue

Hi,

I did a small test regarding Guid serialization with the c# driver 2.11.

Here is my code

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
            BsonDocument doc = new BsonDocument
            {
                {"Standard" ,new BsonBinaryData( new Guid( "1A76AD2A-4FF6-4291-860E-51C5C34AA890"),GuidRepresentation.Standard) },
                {"CSharpLegacy" ,new BsonBinaryData( new Guid( "1A76AD2A-4FF6-4291-860E-51C5C34AA890"),GuidRepresentation.CSharpLegacy) }
            };

When looked the the document , it looked like this:

doc = { {
		"Standard": CSUUID("1a76ad2a-4ff6-4291-860e-51c5c34aa890"),
		"CSharpLegacy": CSUUID("1a76ad2a-4ff6-4291-860e-51c5c34aa890")
	}
}

I would expect it to look like this:

doc = { {
		"Standard": UUID("1a76ad2a-4ff6-4291-860e-51c5c34aa890"),
		"CSharpLegacy": CSUUID("1a76ad2a-4ff6-4291-860e-51c5c34aa890")
	}
}

Am I right?

Thanks,
Itzhak

2 Likes

It looks like that somehow it’s only possible for MongoDB.Driver to have only one type of UUID in a collection. So you just can’t create UUID in one field or object and CSUUID in another.

UPD Source: GuidRepresentationMode

I struggled with this cause I had CSUUID’s in my collection but want to add UUID. In the end I setup MongoCollectionSettings like this

var collectionSettings = new MongoCollectionSettings
{
    GuidRepresentation = GuidRepresentation.Standard
};
var docCollection = Database.GetCollection<BsonDocument>("Document", collectionSettings);

and got an error “GuidRepresentation Standard is only valid with subType UuidStandard, not with subType UuidLegacy”

After that I cleared all data in the the collection and error is gone. Instead of CSUUID my document was saved with UUID