Skip to content

Instantly share code, notes, and snippets.

@bchirico
Created November 21, 2013 11:07
Show Gist options
  • Save bchirico/7579820 to your computer and use it in GitHub Desktop.
Save bchirico/7579820 to your computer and use it in GitHub Desktop.
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client.test
counters = db.counters
def get_next_sequence_number(name):
counter = counters.find_and_modify(query = {'type': name}, update = {'$inc': {'value': 1}}, upsert = True, new = True)
counter_value = counter['value']
return counter_value
print get_next_sequence_number('uid')
print get_next_sequence_number('uids')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment