mongodb - 聚合因内存错误而失败

标签 mongodb aggregation-framework

我正在尝试使用 id 字段在我的分片集合中查找重复项,这是这种模式 -

"id" : {
        "idInner" : {
            "k1" : "v1",
            "k2" : "v2",
            "k3" : "v3",
            "k4" : "v4"
        }
}

我使用了以下查询,但收到“异常:$group 超出内存限制,但不允许外部排序。传递 allowDiskUse:true 以选择加入。”错误,即使我在查询中使用了“allowDiskUse : true”。

db.collection.aggregate([
  { $group: {
    _id: { id: "$id" },
    uniqueIds: { $addToSet: "$_id" },
    count: { $sum: 1 } 
  } }, 
  { $match: { 
    count: { $gte: 2 } 
  } },
  { $sort : { count : -1} },
  { $limit : 10 }
], 
{ 
    allowDiskUse : true
});

有没有其他方法可以得到我想要的东西,或者我应该在上面的查询中传递其他东西?谢谢。

最佳答案

请在运行命令中使用 allowDiskTrue。

db.runCommand(
   { aggregate: "collection",
     pipeline: [
  { $group: {
    _id: { id: "$id" },
    uniqueIds: { $addToSet: "$_id" },
    count: { $sum: 1 } 
  } }, 
  { $match: { 
    count: { $gte: 2 } 
  } },
  { $sort : { count : -1} },
  { $limit : 10 }
],
     allowDiskUse: true
   }
)

让我知道这是否适合您。

关于mongodb - 聚合因内存错误而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35528325/

相关文章:

javascript - Mongoose 选择子文档字段

mongodb 乘法聚合运算符

javascript - 如何在 mongodb 聚合管道中使用 Javascript 对象?

node.js - mongodb 中按日期分组?

javascript - MongoDB原生 Node 驱动: Explain is Broken?

mongodb - 分片集合中 mongodb 中的聚合管道

mongodb - 访问 Azure VM 上的 MongoDB 服务器

mongodb - 无法从 shell 访问集合 - SyntaxError : missing ; before statement (shell):1

mysql - 针对以下场景使用 Count 、 DISTINCT 和 SUM 编写 mongodb 查询

node.js - 无法使用护照在 MongoDB 中存储 session