为什么PyMongo将uuid.uuid1()编码为BSON :: Binary? [英] Why does PyMongo encode uuid.uuid1() as a BSON::Binary?

查看:133
本文介绍了为什么PyMongo将uuid.uuid1()编码为BSON :: Binary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Mongo中的所有文档添加了一个值为uuid.uuid1()的'GUID'键(来自python uuid模块).我注意到它们不是存储为字符串,而是存储为 BSON :: Binary .我已经做了一些谷歌搜索,但是我仍然不了解此序列化的目的/优势是什么.有人可以解释吗?我应该在存储之前将uuid.uuid1()转换为字符串吗?如何使用字符串通过db.myCol.find({'GUID':aString})之类的GUID值来查找()?

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 })?

推荐答案

Python uuid的默认序列化在 BSON规范,因为这可以确保范围查询的排序保持一致,并且还减少了数据/索引的存储空间.

The default serialization for a Python uuid uses a UUID binary 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:

5d78ad35ea5f11e1a183705681b29c47
5D78AD35EA5F11E1A183705681B29C47
5d78ad35ea5f11e1A183705681B29C47

..但是具有与字符串不同的排序顺序:

..but have different sort orders as strings:

> db.uuidsort.find().sort({_id:1})
{ "_id" : "5D78AD35EA5F11E1A183705681B29C47" }
{ "_id" : "5d78ad35ea5f11e1A183705681B29C47" }
{ "_id" : "5d78ad35ea5f11e1a183705681b29c47" }

比较bson大小:

> db.uuidtest.find()
{ "_id" : BinData(3,"XXitNepfEeGhg3BWgbKcRw==") }
{ "_id" : "5d78ad35ea5f11e1a183705681b29c47" }

> Object.bsonsize(db.uuidtest.findOne({_id: BinData(3,"XXitNepfEeGhg3BWgbKcRw==")}))
31

> Object.bsonsize(db.uuidtest.findOne({_id: "5d78ad35ea5f11e1a183705681b29c47"}))
47

如果确实要插入为字符串,则可以使用 UUID. hex 以获取等效的32个字符的字符串:

If you do want to insert as strings, you can use UUID.hex to get the 32-character string equivalent:

>>> db.uuidtest.insert({'_id': uuid.hex})
'5d78ad35ea5f11e1a183705681b29c47'

如果要通过Python从字符串中查找UUID,可以使用 uuid .UUID 方法:

If you want to find UUIDs by string from Python, you can use the uuid.UUID methods:

>>> db.uuidtest.find_one({'_id':uuid.UUID('5d78ad35ea5f11e1a183705681b29c47')})
{u'_id': UUID('5d78ad35-ea5f-11e1-a183-705681b29c47')}

如果要从mongo外壳程序按字符串查找UUID,则有一个UUID()助手:

If you want to find UUIDs by string from the mongo shell, there is a UUID() helper:

> db.uuidtest.find({_id:UUID('5d78ad35ea5f11e1a183705681b29c47')})
{ "_id" : BinData(3,"XXitNepfEeGhg3BWgbKcRw==") }

注意:如 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆