umongo icon indicating copy to clipboard operation
umongo copied to clipboard

old UUID bson subtype used and no string support for UUIDField

Open nmcbride opened this issue 4 years ago • 5 comments

The UUIDField is storing UUIDs as BSON subtype 3. Subtype 3 is known to cause issues with the byte order being different between drivers. This is well documented. Instead BSON subtype 4 should be used and is the default in most driver. Also, it would be nice if the UUIDField also allowed storing the UUID as a string. You could do a binary=true/false as mongoengine does.

nmcbride avatar Jun 04 '20 02:06 nmcbride

I'm sorry I don't know much about BSON subtypes. Could you please elaborate on what happens, what you would expect, and how umongo can help achieving this?

lafrech avatar Jul 20 '20 08:07 lafrech

I'm also learning this stuff. This article explains it better than I can. Basically subtype 3 (legacy) works differently across different drivers. But subtype 4 (newer one) is universal. When I have uMongo create a UUID field, it is creating subtype 3 and there is no way to tell it to create subtype 4.

I was also asking to have a a true / false when create the UUID field to control whether it is stored as binary or as a string.

nmcbride avatar Jul 21 '20 00:07 nmcbride

AFAIU, this is a driver setting (https://api.mongodb.com/python/2.7.2/api/pymongo/collection.html#pymongo.collection.Collection.uuid_subtype).

In other word, this is not something umongo should try to enforce. The user is free to pick the subtype of his choice.

lafrech avatar Sep 01 '20 13:09 lafrech

Ok so you are saying I need to figure out how to set it in asyncio motor (which is the driver I am using) because that's what is setting it to subtype 3 not umongo. Is that correct?

What is your thoughts on the second part of this which is allowing for a switch to specify if a UUID field is to be stored in binary or text form? Originally, I wanted to set the schema up so that the UUID field was storing in String form vs binary but wasn't able to find a switch to do that.

nmcbride avatar Sep 03 '20 00:09 nmcbride

Agreed it's not a umongo issue unless we can "consider" passing some arbitrary/specific args, kwargs to the driver implementations via Meta (DocumentOpts) or something in similar fashion to how pymodm does it.

For now, you can do something as simple as

c = MongoClient(uri)
db = c.get_default_database(codec_options=CodecOptions(uuid_representation=bson.binary.STANDARD)) # or .get_database, etc.
instance = Instance(db)
# ...

or for Motor

c = AsyncIOMotorClient(uri)
db = c.get_default_database(codec_options=CodecOptions(uuid_representation=bson.binary.STANDARD)) # or .get_database, etc.
instance = Instance(db)
# ...

samypr100 avatar Dec 02 '20 00:12 samypr100