MongoDB 3.6.2 $ out失败,重复键错误

时间:2018-11-20 09:15:22

标签: mongodb aggregation-framework

这是聚合查询,意外失败:

db.sq_lesson_user_lessons.aggregate([
  {
   "$match": {
     lesson_id: { 
       "$in": [ObjectId("5bb6ec0a178353bbdecdd94d"), ObjectId("5bbf1e611783538013ce2f0a")] 
     },
     status: { "$in": ['featured','started','pending','completed'] }
   }
  },
  {
   "$project": {
     _id: 1,
     user_profile_id: 1,
     status: 1,
     lesson_id: 1
   }
  },
  {
   "$out": "analytics_company_5bb6039598f17297c964fc54_sq_user_lessons"
  }
])

assert: command failed: {
    "operationTime" : Timestamp(1542715086, 67659),
    "ok" : 0,
    "errmsg" : "insert for $out failed: { lastOp: { ts: Timestamp(1542715086, 67657), t: 39 }, connectionId: 242551, err: \"E11000 duplicate key error collection: api_smartquest_co_production.tmp.agg_out.637145 index: _id_ dup key: { : ObjectId('5bf22e554b8a982ada5e2828') }\", code: 11000, codeName: \"DuplicateKey\", n: 0, ok: 1.0, operationTime: Timestamp(1542715086, 67657), $clusterTime: { clusterTime: Timestamp(1542715086, 67658), signature: { hash: BinData(0, 0000000000000000000000000000000000000000), keyId: 0 } } }",
    "code" : 16996,
    "codeName" : "Location16996",
    "$clusterTime" : {
        "clusterTime" : Timestamp(1542715086, 67659),
        "signature" : {
            "hash" : BinData(0,"wvZz15/714/PHqAWywLpZlP4azQ="),
            "keyId" : NumberLong("6606442824109916161")
        }
    }
} : aggregate failed

此聚合结果大约有30万条记录。有时,附接起作用,有时却失败。

1 个答案:

答案 0 :(得分:1)

MongoDB的Daniel Hatcher回答了以下评论:

https://jira.mongodb.org/browse/SERVER-38212?focusedCommentId=2067603&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-2067603

答案的CRUX是:

由于聚集正在搜索大型集合以返回结果,因此某些文档可能会多次返回。这与MongoDB's read isolation中的概念之一有关。