mongodb - "$push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes"

标签 mongodb configuration

我正在从应用程序发送获取请求并收到以下错误:

{"$push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes"}

我正在使用 atlas 管理的 mongo-db,具有 4GB RAM 和 20GB 存储空间。我想,我需要更新一些配置设置,但我缺少它。谁能帮我解决这个错误。

添加查询

    db.collection1.aggregate([ { '$match': {} }, { '$lookup': { from: 'collection2', localField: 'pfId', foreignField: 'pfId', as: 'pt' } }, { '$unwind': '$pt' }, { '$match': {} }, { '$lookup': { from: 'collection3', localField: 'pt.ptId', foreignField: 'ptId', as: 'pd' } }, { '$unwind': '$pd' }, { '$match': {} }, { '$lookup': { from: 'collection4', localField: 'pd.pdId', foreignField: 'pdId', as: 'dl' } }, { '$unwind': '$dl' }, { '$match': { 'dl.currentdl': { '$ne': 'OBSOLETE' }, '$or': [ { 'pd.pdName': { '$regex': /^.*world.*/i } }, { 'dl.r.dF': { '$regex': /^.*world.*/i } }, { 'dl.dlType': { '$regex': /^.*world.*/i } }, { 'dl.dlId': 'WORLD' }, { 'dl.logUserId': 'WORLD' } ] } }, { '$project': {//some_fields} }, { '$group': { _id: null, count: { '$sum': 1 }, results: { '$push': '$$ROOT' } } }, { '$project': { count: 1, results: { '$slice': [ '$results',
0, 10 ] } } }], { allowDiskUse: true })

我必须对数据进行分页,并检索所获取数据的计数。

最佳答案

所以,经过这么多的努力,这就是我为解决问题所做的事情。 首先要理解的是,我无法在不限制数据的情况下将数据推送到数组中。因此,在对管道中的数据进行分组之前,我使用了 $limit$skip。(这也维护了我的查询的分页)。但问题是如何维护记录总数,因为由于分组前的限制而丢失了记录总数。 因此,我找到的解决方案是使用 $facet,它可以帮助我在同一聚合管道中实现两个管道。

db.prdfam.aggregate([{
$facet: {
"counts":[
{ '$match': {} },
{ '$lookup': { from: 'pt', localField: 'pfId', foreignField: 'pfId', as: 'pt' } },
{ '$unwind': '$pt' }, { '$match': {} }, 
{ '$lookup': { from: 'prds', localField: 'pt.ptId', foreignField: 'ptId', as: 'prd' } },
{ '$unwind': '$prd' }, { '$match': {} }, 
{ '$lookup': { from: 'del', localField: 'prd.prdId', foreignField: 'prdId', as: 'delivery' } }, 
{ '$unwind': '$delivery' }, { '$match': { 'delivery.currentDelivery': { '$ne': 'OBSOLETE' }, 
'$or': [ { 'prd.prdName': { '$regex': /^.*world.*/i } },
{ 'delivery.rInfo.dataFormat': { '$regex': /^.*world.*/i } },
{ 'delivery.dType': { '$regex': /^.*world.*/i } }, 
{ 'delivery.dId': 'WORLD' }, { 'delivery.UserId': 'WORLD' } ] } }, 
{ '$group': { _id: null, count: { '$sum': 1 } } }
],
"results":[
//same lookup & match conditions as in above element 
{ '$project': { //fields to project } }, 
{ '$sort': { updatedAt: -1 } }, {$skip: 0},{ $limit : 10 },
{ '$group': { _id: null, results: { '$push': '$$ROOT' } } },
{ '$project': { results: 1 } }
]
}
}], 
{ allowDiskUse: true })

希望这对其他人有帮助。干杯:)

关于mongodb - "$push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59714842/

相关文章:

来自 Grails 的 MongoDB 'upsert'

postgresql - 大表查询的第一次调用出奇慢

c# - App.config 与 .ini 文件

java - 可执行 JAR 和 Ant

mongodb - MongoError : connection destroyed, 无法实例化游标

MongoDB:无法规范化查询:BadValue $not 需要正则表达式或文档

javascript - 如何在查询中获取查询对象?

node.js - Mongodb图集未连接

configuration - TeamCity 的版本控制(例如在 TFS 中)构建配置 - 可能吗?

Java 更改服务器和数据库设置以确保安全