MongoDB Compass:显示二进制类型3的guid



我有一个python软件,使用以下代码在mongo数据库中插入uid:

class User(mongoengine.Document):
id = mongoengine.UUIDField(primary_key=True, required=True, default=lambda: str(uuid.uuid4()))

在MongoDB Compass中,它显示为:

_id: Binary('ytSLGP47REmESqyzow9kAw==', 3)

"_id": {
"$binary": "ytSLGP47REmESqyzow9kAw==",
"$type": "3"
},

我可以强制MongoDB指南针显示这些字段作为常规uid不修改我的数据库?

我没有找到使用mongoengine解决这个问题的方法,但是我会给你一个方法,但是使用不同的python库pymongo

<标题>

PYMONGO解用这种方法将其记录为UUID对象,因此我认为它可以帮助您解决您的问题。

第一步:

  • 获取mongo客户端
  • 让你的集合对象使用它

注释流很好地解释了这一点。

from pymongo import MongoClient
from bson.binary import UuidRepresentation
from uuid import uuid4
# use the 'standard' representation for cross-language compatibility.
client = MongoClient(uuidRepresentation='standard')
collection = client.get_database('database_name').get_collection('collection_name')
# create a native uuid object
uuid_obj = uuid4()
# save the native uuid object to MongoDB
collection.insert_one({'uuid': uuid_obj})
# To test if everything worked well and look up the results.
# retrieve the stored uuid object from MongoDB
document = collection.find_one({})
# check that the retrieved UUID matches the inserted UUID
assert document['uuid'] == uuid_obj
在<<p> strong> MONGODB 必须给我们看
{'_id': UUID('00112233-4455-6677-8899-aabbccddeeff')}

作为十六进制UUID一个随机生成的UUID。

最新更新