MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

come convertire una stringa esadecimale in ObjectId in Python

Importa ID oggetto:

from bson import ObjectId

Da ObjectId a stringa:

oid = ObjectId()
oid_str = str(oid)
# oid_str is now '555fc7956cda204928c9dbab'

Da stringa a ObjectId:

oid_str = '555fc7956cda204928c9dbab'
oid2 = ObjectId(oid_str)
print(repr(oid2))
# ObjectId('555fc7956cda204928c9dbab')