Skip to content

Instantly share code, notes, and snippets.

@lardissone
Created March 14, 2012 01:35
Show Gist options
  • Save lardissone/2033257 to your computer and use it in GitHub Desktop.
Save lardissone/2033257 to your computer and use it in GitHub Desktop.
Implementation of auto_increment for MongoDB
"""
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