Skip to content

Instantly share code, notes, and snippets.

@fmasanori
Last active March 5, 2018 13:20
Show Gist options
  • Save fmasanori/5923187 to your computer and use it in GitHub Desktop.
Save fmasanori/5923187 to your computer and use it in GitHub Desktop.
find and modify mongo
import pymongo
def get_next_sequence_number(name):
connection = pymongo.MongoClient("mongodb://localhost")
db = connection.test
counters = db.counters
counter = counters.find_and_modify(query={'type':name},
update={'$inc':{'value':1}},
upsert=True, new=True)
return counter['value']
print (get_next_sequence_number('uid'))
print (get_next_sequence_number('uid'))
print (get_next_sequence_number('uid'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment