MongoDb UUID storage type support
randrewy opened this issue · 6 comments
There is a way to set guid db representation using C# MongoDb client using GuidSerializer
Is there anything like that option in MongoDB Storage Provider? Any plans to add it in future?
I've tried both [BsonGuidRepresentation(GuidRepresentation.Standard)]
annotation on my Guid
field and global BsonDefaults.GuidRepresentation = GuidRepresentation.Standard;
setting, but neither of them worked for me.
What is the current represenation?
Currently it is stored as string like .ToString
was called on guid object: "db331560-56c6-4a35-84cc-1846d0d5cc7e"
This library does not use mongo serializer. It uses Newtonsoft.JSON and then converts it to BSON. So you can write your own serializer if you need another representation. But binary data is not supported.
Actually I can write custom converter to store Guid as binary data, but that means I have to
- manually decide how to order bytes in represenatation (.Net uses non-rfc byte order if you call
.ToByteArray()
) - manually encode my guid as bytes if I want to perform a query from mongoDbClient shell
I've looked through the code and found this converter you are talking about and there is this special case for Guid
type
I can confirm that Newtonsoft.Json library recognize guids and serialize them with proper
JTokenType.Guid
MongoDb Driver has it's own converter in form of Implicit Conversion (Guid to BsonValue) and it should tolerate at least global
BsonDefaults.GuidRepresentation
settings (I've never checked that).Edit: implicit conversion is deprecated and
BsonBinaryData(Guid guid, GuidRepresentation representation)
should be used.
So it looks like there is everything needed to implement this feature. The only remaining questions are: is is it planned for implementation? PR are welcomed?
I do not see that much benefit in a byte representation. It is so much harder to debug.
But you are welcome to provide a PR, but it must be backwards compatible. BsonDefaults.GuidRepresentation
is therefore not an option.
I see 2 options:
- Just provide a flag for the line you mentioned. But you have to think about how to do the deserialization.
- Provide a flag to use Mongo Serialization instead of the conversion.
Is this something you want to work on or can we close the issue?