【问题标题】:mongoose allowDiskUse(true) not working nodejs猫鼬allowDiskUse(真)不工作nodejs
【发布时间】:2020-12-28 22:39:37
【问题描述】:

所以我有以下代码:

pastlocation.aggregate([
        {
          '$project': {
            'location': 1, 
            'month': {
              '$month': '$timestamp'
            }
          }
        }, {
          '$match': {
            'month': 11
          }
        },{
          '$group': {
            '_id': '$location.coordinates', 
            'count': {
              '$sum': 1
            }
          }
        }, {
          '$project': {
            '_id': 0, 
            'count': 1, 
            'location.coordinates': {
              '$map': {
                'input': '$_id', 
                'in': {
                  '$toString': '$$this'
                }
              }
            }
          }
        },{$sort:{count:-1}}
      ]).allowDiskUse(true).exec(function(err,result) {

        if(err){console.log(err)}
     
      console.log(result)
      res.json(result);
      
  });

从我读到的所有内容来看,这是在 5.11.8 版本中使用 Mongoose 时使用 allowDiskUse 的正确方法

https://mongoosejs.com/docs/api.html#aggregate_Aggregate-allowDiskUse

但我似乎收到此错误消息。

MongoError: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting.
    at MessageStream.messageHandler (/home/russellharrower/Documents/NodeApps/AdStichr-Server/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/home/russellharrower/Documents/NodeApps/AdStichr-Server/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/russellharrower/Documents/NodeApps/AdStichr-Server/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:716:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
    at TLSSocket.Readable.push (_stream_readable.js:212:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23) {
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1609147254 },
  ok: 0,
  code: 16819,
  codeName: 'Location16819',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1609147254 },
    signature: { hash: [Binary], keyId: [Long] }
  }
}

我正在使用 MongoDB Atlas - 是否有需要打开的设置或遗漏了什么?

【问题讨论】:

  • 它似乎正在工作,但管道阶段的问题取决于索引和数据的大小。我建议检查您正在匹配和排序的字段的索引,并尝试减少管道。如果可以的话,你可以直接使用 match 并删除第一个投影管道。
  • @MohamedKamel 感谢您的评论 - 您的意思是将日期部分放在主项目中并删除顶部的部分。问题是这个集合有超过 600,000 个文档。

标签: node.js mongodb mongoose


【解决方案1】:

检查排序缓冲区大小:

db.runCommand( { getParameter : 1, "internalQueryExecMaxBlockingSortBytes" : 1 } )

(默认情况下,mongod 是 32MB,但似乎 ATLAS 是 100MB) 例如,您可以尝试将其增加到 300MB:

db.adminCommand({setParameter: 1, internalQueryExecMaxBlockingSortBytes: 314572800})

也不确定您的架构,但也许 $match 阶段可以移动到 $project 之前,并且 $project 只允许上述情况的 location.coordinates ...

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 2018-05-02
    • 2017-05-26
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 2013-11-01
    相关资源
    最近更新 更多