Pymongo Python中UUID到NUUID的转换

Pymongo Python中UUID到NUUID的转换,pymongo,uuid,Pymongo,Uuid,在我的应用程序中,我使用PyMSSQL从MSSQL获取一些值。Python将其中一个值解释为UUID。我把这个值赋给了一个名为id的变量 print (type(id),id) 我明白了 这是因为我需要将UUID转换为NUUID 注意:我也试过了 client.db.collectionname.find_one({"_id" : NUUID("cc26ce03-b6cb-4a90-9d0b-395c313fc968")}) 但它不起作用。有什么想法吗?假设您使用的是PyMongo 3.x:

在我的应用程序中,我使用PyMSSQL从MSSQL获取一些值。Python将其中一个值解释为UUID。我把这个值赋给了一个名为id的变量

print (type(id),id)
我明白了

这是因为我需要将UUID转换为NUUID

注意:我也试过了

client.db.collectionname.find_one({"_id" : NUUID("cc26ce03-b6cb-4a90-9d0b-395c313fc968")})

但它不起作用。有什么想法吗?

假设您使用的是PyMongo 3.x:

from bson.binary import CSHARP_LEGACY
from bson.codec_options import CodecOptions
options = CodecOptions(uuid_representation=CSHARP_LEGACY)
coll = client.db.get_collection('collectionname', options)
coll.find_one({"_id": id})
如果您使用的是PyMongo 2.x

from bson.binary import CSHARP_LEGACY
coll = client.db.collectionname
coll.uuid_subtype = CSHARP_LEGACY
coll.find_one({"_id": id})
您必须告诉PyMongo UUID最初存储的格式。还有一个JAVA_遗留表示

from bson.binary import CSHARP_LEGACY
from bson.codec_options import CodecOptions
options = CodecOptions(uuid_representation=CSHARP_LEGACY)
coll = client.db.get_collection('collectionname', options)
coll.find_one({"_id": id})
from bson.binary import CSHARP_LEGACY
coll = client.db.collectionname
coll.uuid_subtype = CSHARP_LEGACY
coll.find_one({"_id": id})