MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

comment convertir une chaîne hexadécimale en ObjectId en Python

Importer l'ID d'objet :

from bson import ObjectId

De ObjectId à chaîne :

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

De chaîne à ObjectId :

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