【问题标题】:Java/Grails - MongoDB aggregation 16MB buffer size limitJava/Grails - MongoDB 聚合 16MB 缓冲区大小限制
【发布时间】:2015-10-14 11:01:19
【问题描述】:

我正在尝试从 java 运行 mongo db 聚合查询,但缓冲区大小超过 16MB。有没有办法调整缓冲区大小或任何其他解决方法。我没有在 mongo 服务器端创建集合的选项,而且我的客户端系统中没有任何 mongo 实用程序,如 mongo.exe 或 mongoExport.exe。

这里是一小部分代码

if (!datasetObject?.isFlat && jsonFor != 'collection-grid'){
   //mongoPipeline = new AggregateArgs (Pipeline = pipeline, AllowDiskUse = true, OutputMode = AggregateOutputMode.Cursor)
   output= dataSetCollection.aggregate(pipeline)
}else{
     output= dataSetCollection.aggregate(project)
    }

我有 30 个字段的 100K 记录。当我为所有 100K 记录查询 5 个字段 时,我得到了结果(成功)。但是当我查询所有字段的 100K 记录时,它会抛出错误。

问题是当我尝试访问集合中的所有文档时,包括文档的 所有字段 超过 16Mb 的限制大小。

实际错误:

com.mongodb.CommandFailureException: { "serverUsed" : "127.0.0.1:27017" , "errmsg" : "exception: aggregation result exceeds maximum document size (16MB)" , "code" : 16389 , "ok" : 0.0

如何解决这个问题?

使用 MongoDB-3.0.6

注意:GridFS 不适合我的标准。因为我需要在一个请求中检索所有文档,而不是一个文档。

【问题讨论】:

  • 嗯,不。在不分享您当前正在尝试的代码的情况下,获得更好方法建议的可能性也为零。
  • @BlakesSeven 查看更新的问题我保留了一些代码。

标签: java mongodb grails aggregation-framework mongodb-java


【解决方案1】:

运行聚合时,您可以告诉 mongo 返回一个游标。使用 3.0 Java 驱动程序中的新 API,如下所示:

// Assuming MongoCollection
dataSetCollection.aggregate(pipeline).useCursor(true)

您可能还需要告诉它使用服务器上的磁盘空间,而不是全部在内存中完成:

// Assuming MongoCollection
dataSetCollection.aggregate(pipeline).useCursor(true).allowDiskUse(true)

如果您使用的是较旧的驱动程序(或新驱动程序中的旧 API),这两个选项将如下所示:

// Assuming DBCollection
dataSetCollection.aggregate(pipeline, AggregationOptions.builder()
    .allowDiskUse(true)
        .useCursor(true)
        .build())
    .useCursor(true)

【讨论】:

  • 我正在使用 mongodb-win32-i386-3.0.6
  • 它不工作。我尝试使用这个 dataSetCollection.aggregate(pipeline).useCursor(true).allowDiskUse(true)。即使对于 的小数据,它现在也给出相同的错误
  • 无方法签名:com.mongodb.AggregationOutput.useCursor() 适用于参数类型:(java.lang.Boolean) 值:[true] @evanchooly
  • 然后试试第二种形式。听起来您使用的是较旧的 API。
  • 抱歉,您说的是哪个 API
【解决方案2】:

有两种方法可以解决这个问题

1) 使用$out 创建新的集合并写入结果,这不是一个好主意,因为此过程耗时且实施复杂。

public class JavaAggregation {
public static void main(String args[]) throws UnknownHostException {

    MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("databaseName");

    DBCollection coll = db.getCollection("dataset");

    /*
        MONGO SHELL : 
        db.dataset.aggregate([ 
            { "$match": { isFlat : true } }, 
            { "$out": "datasetTemp" }
        ])
    */

    DBObject match = new BasicDBObject("$match", new BasicDBObject("isFlat", true)); 
    DBObject out = new BasicDBObject("$out", "datasetTemp"); 

    AggregationOutput output = coll.aggregate(match, out);

    DBCollection tempColl = db.getCollection("datasetTemp");
    DBCursor cursor = tempColl.find();

    try {
        while(cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } finally {
        cursor.close();
    }
 }
}

2。使用allowDiskUse(true) 实现起来非常简单,甚至不费时。

public class JavaAggregation {
public static void main(String args[]) throws UnknownHostException {

    MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("databaseName");

    DBCollection coll = db.getCollection("dataset");

    /*
        MONGO SHELL : 
        db.dataset.aggregate([ 
            { "$match": { isFlat : true } }, 
            { "$out": "datasetTemp" }
        ])
    */

    DBObject match = new BasicDBObject("$match", new BasicDBObject("isFlat", true)); 
    def dbObjArray = new BasicDBObject[1]
    dbObjArray[0]= match
    List<DBObject> flatPipeline = Arrays.asList(dbObjArray)

    AggregationOptions aggregationOptions = AggregationOptions.builder()
                                    .batchSize(100)
                                    .outputMode(AggregationOptions.OutputMode.CURSOR)
                                    .allowDiskUse(true)
                                    .build();
    def cursor = dataSetCollection.aggregate(flatPipeline,aggregationOptions)
    try {
        while(cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    } 
    finally {
        cursor.close();
    }
}

更多信息请参见herehere

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 2013-02-24
    • 1970-01-01
    • 2017-12-26
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多