2010-10-07 python mongodb findandmodify
Пример получения уникального инкрементируемого числа
# coding:utf8
from pymongo.connection import Connection
db = Connection('localhost')['test']
def get_autoincrement():
result = db.command('findandmodify', 'test',
query={ 'name': 'id' }, update={ '$inc': { 'val':1 } } )
if not int(result['ok']): return None
return result['value']['val']
db.test.update({ 'name':'id' },{ 'name':'id', 'val':1 }, True)
print 'before',db.test.find_one()['val']
print 'capture id = ',get_autoincrement()
print 'after',db.test.find_one()['val']
before 1
capture id = 1
after 2