Skip to content

Instantly share code, notes, and snippets.

@alexandre
Created June 27, 2014 23:47
Show Gist options
  • Save alexandre/6ff7824f6cd4f9c76f8a to your computer and use it in GitHub Desktop.
Save alexandre/6ff7824f6cd4f9c76f8a to your computer and use it in GitHub Desktop.
Converter DBRef em um document para ObjectId.
#Usando mongoengine...
def dbref_to_objid(document):
'''
Recebe um documento como parametro e converte os DBRefs para
ObjectId.
'''
from bson import DBRef
doc_copy = document._data.copy()
for key, field in document._data.items():
if isinstance(field, DBRef):
doc_copy[key] = field.id
return doc_copy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment