I’m adding a ‘GUID’ key with a value of uuid.uuid1() (from python uuid module) for all my documents in Mongo. I noticed they are being stored not as strings, but as type BSON::Binary. I’ve done some Googling already, but I still don’t understand what the purpose/advantage to this serialization is. Can someone explain? Should I be converting the uuid.uuid1() to strings before storing? How can I use a string to find() by the GUID value like db.myCol.find({ ‘GUID’ : aString })?
Share
The default serialization for a Python
uuiduses aUUIDbinary representation in the BSON spec because this ensures consistent sorting for range queries, and also uses less storage for data/indexes.For example, these three strings are equivalent in hex:
..but have different sort orders as strings:
Comparing the bson sizes:
If you do want to insert as strings, you can use UUID.hex to get the 32-character string equivalent:
If you want to find UUIDs by string from Python, you can use the uuid.UUID methods:
If you want to find UUIDs by string from the
mongoshell, there is aUUID()helper:Note: there are a few other UUID subtypes available for interoperability with other driver versions, as described in the API docs for bson.binary.