Created
March 14, 2012 01:35
-
-
Save lardissone/2033257 to your computer and use it in GitHub Desktop.
Implementation of auto_increment for MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
db.seq.insert({_id: 'pacientes', seq: 1}) | |
""" | |
from django.conf import settings | |
from pymongo import Connection | |
DATABASES = settings.DATABASES | |
connection = Connection(DATABASES['default']['HOST'], DATABASES['default']['PORT']) | |
mongo = connection[DATABASES['default']['NAME']] | |
if len(DATABASES['default']['USER']) > 0: | |
mongo.authenticate(DATABASES['default']['USER'], DATABASES['default']['PASSWORD']) | |
def auto_inc(collection='pacientes'): | |
new_id = mongo.seq.find_and_modify( | |
query={'_id': collection}, | |
update={'$inc': {'seq': 1}}, | |
new=True | |
) | |
return new_id['seq'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment