Last active
          March 5, 2018 13:20 
        
      - 
      
- 
        Save fmasanori/5923187 to your computer and use it in GitHub Desktop. 
    find and modify mongo
  
        
  
    
      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
    
  
  
    
  | 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