mongodb 不可能 (?) E11000 duplicate key error dup key when upserting

mongodb impossible (?) E11000 duplicate key error dup key when upserting

我的理解是,在单个文档上使用 upsert:true 更新是一个原子操作,因此这永远不会导致重复键错误,尤其是在主 _id 键上,当集合没有唯一性时 - ly 索引字段:

Order.update({ _id: order._id }, query, { upsert: true }, cb) // with mongoose

但这出现在 mongod.log:

    2015-03-27T09:39:10.349-0400 I WRITE    [conn258236] update xyz.orders 
query: { _id: "6353f880-c6a7-4260-809f-98e0af27b9a2" } update: { $set: { ... 
} keyUpdates:0 writeConflicts:0 **exception: E11000 duplicate key error dup 
key: { : "6353f880-c6a7-4260-809f-98e0af27b9a2" } code:11000** numYields:1 
locks:{} 138ms


    2015-03-27T09:39:10.349-0400 I COMMAND  [conn258236] command xyz.$cmd 
command: update { update: "orders", writeConcern: { w: 1 }, ordered: true, 
updates: [ { q: { _id: "6353f880-c6a7-4260-809f-98e0af27b9a2" }, u: { $set: { 
... } }, multi: false, upsert: true } ] } keyUpdates:0 writeConflicts:0 
numYields:0 reslen:235 locks:{} 139ms

这是 db.orders.getIndexes() 的输出:

{
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "xyz.orders"
},

我们正在使用 MongoDB 版本 3.0.0 和 WiredTiger。

恐怕这是一个持续存在的问题。我有同样的问题,我找到了一张关于这个的 jira 票:

https://jira.mongodb.org/browse/SERVER-14322

It is possible that two updates come in with upsert:true, resulting in neither finding a document and both inserting new documents which conflict on unique index violations of the query predicate.

这里的"solution"是在客户端添加重试码