栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > MongoDB

mongodb内存不足怎么解决?

MongoDB 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);

更多mongodb相关文章请关注python自学网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/13993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号