mongodb記憶體不足怎麼解決?

lee_lgw發表於2021-09-11

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相關文章請關注。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/1020/viewspace-2833444/,如需轉載,請註明出處,否則將追究法律責任。

相關文章