小编mem*_*box的帖子

多进程Upsert重复键错误(Mongo> = 3.0.4 WiredTiger)

所有

我刚从我们的applcation发出了一个奇怪的错误:

当我用两个进程更新时,它抱怨一个具有唯一索引的集合上的重复键错误,但是有问题的操作是一个upsert.

案例代码:

import time
from bson import Binary
from pymongo import MongoClient, DESCENDING

bucket = MongoClient('127.0.0.1', 27017)['test']['foo']
bucket.drop()
bucket.update({'timestamp': 0}, {'$addToSet': {'_exists_caps': 'cap15'}}, upsert=True, safe=True, w=1, wtimeout=10)
bucket.create_index([('timestamp', DESCENDING)], unique=True)
while True:
    timestamp =  str(int(1000000 * time.time()))
    bucket.update({'timestamp': timestamp}, {'$addToSet': {'_exists_foos': 'fooxxxxx'}}, upsert=True, safe=True, w=1, wtimeout=10)
Run Code Online (Sandbox Code Playgroud)

当我用两个进程运行脚本时,Pymongo Exception:

Traceback (most recent call last):
  File "test_mongo_update.py", line 11, in <module>
    bucket.update({'timestamp': timestamp}, {'$addToSet': {'_exists_foos': 'fooxxxxx'}}, upsert=True, safe=True, w=1, wtimeout=10)
  File "build/bdist.linux-x86_64/egg/pymongo/collection.py", line 552, in update
  File "build/bdist.linux-x86_64/egg/pymongo/helpers.py", line 202, …
Run Code Online (Sandbox Code Playgroud)

mongodb

10
推荐指数
2
解决办法
5320
查看次数

标签 统计

mongodb ×1