Mongo 聚合因“超出 $group 的内存限制”而失败

如何解决Mongo 聚合因“超出 $group 的内存限制”而失败

我们有一个获取最小和最大纬度/经度的查询。我们为此使用聚合查询。我们有 200 万份文档。

我们在运行聚合查询时遇到以下错误。我们怎样才能解决这个问题?如果我们使用allowdiskUse:true,性能会下降吗?或者我们可以添加一些索引来解决这个问题吗?

2021-04-02T23:57:16.682+0000 I COMMAND  [conn2829719] command loc-service.locations command: aggregate { aggregate: "locations",pipeline: [ { $match: { customerId: "8047380094" } },{ $unwind: "$outdoorLocationInfo.location.coordinates" },{ $group: { _id: "$_id",longitude: { $first: "$outdoorLocationInfo.location.coordinates" },latitude: { $last: "$outdoorLocationInfo.location.coordinates" } } },{ $group: { _id: null,minLongitude: { $min: "$longitude" },maxLongitude: { $max: "$longitude" },minLatitude: { $min: "$latitude" },maxLatitude: { $max: "$latitude" } } } ],cursor: {},allowdiskUse: false,$db: "loc-service",$clusterTime: { clusterTime: Timestamp(1617407827,2),signature: { hash: BinData(0,F980F28628AF21C214BD2D3F4B7C48F56ACB47BD),keyId: 6914764447386959875 } },lsid: { id: UUID("a6e20fee-7714-4460-bdc8-2019425c7ff0") } } planSummary: IXSCAN { customerId: 1,deviceid: 1 } numYields:7900 ok:0 errMsg:"Exceeded memory limit for $group,but didn't allow external sort. Pass allowdiskUse:true to opt in." errName:Location16945 errCode:16945 reslen:313 locks:{ Global: { acquireCount: { r: 8061 } },Database: { acquireCount: { r: 8060 } },Collection: { acquireCount: { r: 8060 } } } storage:{} protocol:op_msg 5448ms

查询

db.locations.aggregate([
  {
    $match: {
      customerId: "8047380094"
    }
  },{
    $unwind: "$outdoorLocationInfo.location.coordinates"
  },{
    $group: {
      _id: "$_id",longitude: {
        $first: "$outdoorLocationInfo.location.coordinates"
      },latitude: {
        $last: "$outdoorLocationInfo.location.coordinates"
      }
    }
  },{
    $group: {
      _id: null,minLongitude: {
        $min: "$longitude"
      },maxLongitude: {
        $max: "$longitude"
      },minLatitude: {
        $min: "$latitude"
      },maxLatitude: {
        $max: "$latitude"
      }
    }
  }
])

我们对这个集合的索引:

db.locations.getIndexes()
[
    {
        "v" : 2,"key" : {
            "_id" : 1
        },"name" : "_id_","ns" : "loc-service.locations"
    },{
        "v" : 2,"key" : {
            "customerId" : 1,"deviceid" : 1
        },"name" : "customerId_1_deviceid_1","ns" : "loc-service.locations","sparse" : true,"background" : true
    },"geoHash" : 1
        },"name" : "customerId_1_geoHash_1","outdoorLocationInfo.location" : "2dsphere"
        },"name" : "customerId_1_outdoorLocationInfo.location_2dsphere","background" : true,"2dsphereIndexVersion" : 3
    },"outdoorLocationInfo.location.coordinates" : 1
        },"name" : "customerId_1_outdoorLocationInfo.location.coordinates_1","background" : true
    }
]

样本数据:

db.locations.findOne()
{
    "_id" : ObjectId("60551b70a48edf83848607d2"),"outdoorLocationInfo" : {
        "location" : {
            "type" : "Point","coordinates" : [
                -95.330024,36.262476
            ]
        }
    },"customerId" : "2868306879","deviceid" : "6eN7sMEOP1e","geoHash" : "9yknq9qu1rqp",}

谢谢

解决方法

我认为您可以使用 $arrayElemAt

简化您的查询
db.collection.aggregate([
  {
    $match: {
      customerId: "8047380094"
    }
  },{
    $group: {
      _id: null,"maxLatitude": {
        "$max": {
          "$arrayElemAt": [
            "$outdoorLocationInfo.location.coordinates",1
          ]
        }
      },"maxLongitude": {
        "$max": {
          "$arrayElemAt": [
            "$outdoorLocationInfo.location.coordinates",0
          ]
        }
      },"minLatitude": {
        "$min": {
          "$arrayElemAt": [
            "$outdoorLocationInfo.location.coordinates","minLongitude": {
        "$min": {
          "$arrayElemAt": [
            "$outdoorLocationInfo.location.coordinates",}
  }
])

试试here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?