4

I have a python software that inserts uuids in a mongo database using this code:

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

In MongoDB Compass, it is shown as:

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

or

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

Can I force MongoDB Compass to show these fields as regular uuids without modifying my database?

Kiruahxh
  • 1,276
  • 13
  • 30

1 Answers1

0

I didn´t find a way to solve that issue using mongoengine but I´ll give you a way to do it but with a different python library pymongo.

PYMONGO SOLUTION

Using this way record it as an UUID object, so i think it can help you with your issues.

First steps:

  • Get your mongo client
  • Get your collection object to work with it

It is well explained by the comments flow.

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

In MONGODB it will have to be shown us

{'_id': UUID('00112233-4455-6677-8899-aabbccddeeff')}

Being that hexadecimal UUID one random generated uuid.

mrCatlost
  • 166
  • 9
  • MongoEngine seems to support standard UUID though they do not advertise it, using `mongoengine.connect(db='mydatabase', uuidRepresentation='standard')` https://github.com/MongoEngine/mongoengine/issues/2296#issuecomment-605452193 – Kiruahxh Apr 12 '23 at 09:08
  • Great, you solve it using that? @Kiruahxh, if that´s right tell me know and i add to this answer and we can let it as the answer to your question for the community :)! – mrCatlost Apr 12 '23 at 10:52
  • Not really. The project I work on uses type 3 UUIDS which seem reliable except that Compass refuses to display them. I am searching for a way to browse the data, or an easy migration path ; not for the correct setup of a brand new project. For example, this migration step is interesting but partial because it only handles `_id` field: https://stackoverflow.com/questions/48402816/migrate-from-legacy-uuid-to-standard-uuid I wonder if migration is necessary and why mongoengine's documentation doesn't mention it – Kiruahxh Apr 12 '23 at 12:30
  • A migration script which scans every mongo document recursively would be appreciated. – Kiruahxh Apr 12 '23 at 12:32
  • Here is a recursive migration script: https://gist.github.com/nmoreaud/f2fab5b76faac33c36d93ec3be4de991 – Kiruahxh Apr 12 '23 at 13:58
  • Thx!!! During these days i´ll add to the answer :)! – mrCatlost Apr 13 '23 at 12:26