MongoDB 3.6.2 $out 因重复键错误而失败

标签 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 万条记录。有时聚合有效,有时失败。

最佳答案

Daniel Hatcher 回答了这个问题来自 MongoDB,带有以下评论:

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

答案的关键是:

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

关于MongoDB 3.6.2 $out 因重复键错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389686/

相关文章:

javascript - Mongoose/MongoDB - 更新子文档不起作用

MongoDB - 使用聚合框架或 mapreduce 匹配文档中的字符串数组(配置文件匹配)

mongodb - 蒙戈 : get hashmap in aggregate

python - PyMongo Aggregate 如何获取executionStats

arrays - MongoDB 多个嵌套数组的大小

javascript - 在 Meteor 应用程序中将名称映射到 id

MongoDB - 对象?为什么我需要 _id 汇总

javascript - 如何在html页面或jade页面中显示mongoose结果

javascript - 如何将项目插入 Mongoose 对象内的数组?

mongodb - 数组项上的 $dateToString