Skip to content

Instantly share code, notes, and snippets.

@g-k
Created April 21, 2011 03:56
Show Gist options
  • Save g-k/933686 to your computer and use it in GitHub Desktop.
Save g-k/933686 to your computer and use it in GitHub Desktop.
pymongo uuid.uuid support
import uuid
import pymongo
if __name__ == '__main__':
hasuuid = lambda : { 'my_uuid' : uuid.uuid4() }
c = pymongo.Connection()
db = c.test # use test database
uuids = db.uuids # use uuids collection
uuids.insert([hasuuid(), hasuuid()])
for uid in uuids.find():
print uid, type(uid['my_uuid'])
# Prints something like:
# {u'_id': ObjectId('4dafa1699f83560689000000'), u'my_uuid': UUID('1c8f6560-13ae-4b29-82e5-e369416c8a57')} <class 'uuid.UUID'>
# {u'_id': ObjectId('4dafa1699f83560689000001'), u'my_uuid': UUID('7a425176-dc00-4606-9e3e-914effd874c0')} <class 'uuid.UUID'>
# > db.uuids.find();
# { "_id" : ObjectId("4dafa2e09f835606be000000"), "my_uuid" : BinData(3,"SPpf+vAQQNesHEafDBY08Q==") }
# { "_id" : ObjectId("4dafa2e09f835606be000001"), "my_uuid" : BinData(3,"XI81oyMQToS8ehtnjb12Dw==") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment