加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 服务器 > 系统 > 正文

MongoDB内存不够怎么情况,怎么解决

发布时间:2023-09-01 15:16:26 所属栏目:系统 来源:
导读:这篇文章主要介绍“MongoDB内存不足怎么情况,怎么解决”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“MongoDB内存不足怎么情况,怎么解决”文章
这篇文章主要介绍“MongoDB内存不足怎么情况,怎么解决”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“MongoDB内存不足怎么情况,怎么解决”文章能帮助大家解决问题。

mongodb每一个文档默认只有16M。聚合的结果是一个BSON文档,当超过16M大小时,就会报内存不够错误。
 
exceeded memory limit for $group.but didn't allow external sort.
 
可以采用打开使用磁盘来解决大小问题。例如
 
db.flowlog.aggregate([{$group:{_id:"$_id"}}], {allowDiskUse: true})
 
java代码片段
 
AggregationOptions options = new AggregationOptions.Builder().allowDiskUse(true).build();
 
Aggregation agg = Aggregation.newAggregation().withOptions(options);
 
但是如果结果集超过了16M,那么依然会报错误。
 
采用一个下面的聚合方法
 
Aggregation agg = Aggregation.newAggregation(
 
                    Aggregation.group(field1
 
                            , field2
 
                            , field3)
 
                            .sum(field4).as("sampleField1")
 
                            .sum(field5).as("sampleField2"),
 
                    Aggregation.project(field4, field5),
 
                    new AggregationOperation() {
 
                        @Override
 
                          public DBObject toDBObject(AggregationOperationContext context) {
 
                            return new BasicDBObject("$out", "test");
 
                        }
 
                    }).withOptions(options);
 
  mongo.aggregate(agg, sourceCollection, Test.class);
 
 如果要在聚合的时候增加一个常量,可采用以下形式
 
Aggregation agg = Aggregation.newAggregation(
 
                    Aggregation.group(
 
                            , OnofflineUserHistoryField.MAC
 
                            , StalogField.UTC_CODE)
 
                            .sum(OnofflineUserHistoryField.WIFI_UP_DOWN).as(OnofflineUserHistoryField.WIFI_UP_DOWN)
 
                            .sum(OnofflineUserHistoryField.ACTIVE_TIME).as(OnofflineUserHistoryField.ACTIVE_TIME),
 
                    Aggregation.project("mac","buildingId","utcCode",OnofflineUserHistoryField.ACTIVE_TIME, OnofflineUserHistoryField.WIFI_UP_DOWN).and(
 
                    new AggregationExpression() {
 
                        @Override
 
                        public DBObject toDbObject(AggregationOperationContext context) {
 
                            return new BasicDBObject(
 
                                    "$cond", new Object[]{
 
                                            new BasicDBObject(
 
                                                "$eq", new Object[]{ "$tenantId", 0}
 
                                            ),
 
                                            20161114,
 
                                            20161114
 
                                     });
 
                        }
 
                    }).as("day").andExclude("_id"),            或者
 
                      and(new AggregationExpression() {
 
             @Override
 
             public DBObject toDbObject(AggregationOperationContext context) {
 
                         return new BasicDBObject("$add", new Object[] { 20141114 });
 
            }  
 
                    }).as("day").andExclude("_id"),
 
            new AggregationOperation() {
 
                        @Override
 
                          public DBObject toDBObject(AggregationOperationContext context) {
 
                            return new BasicDBObject("$out", "dayStaInfoTmp");
 
                        }
 
                    }).withOptions(options);
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章