Java/Grails - MongoDB 聚合 16MB 缓冲区大小限制

Java/Grails - MongoDB aggregation 16MB buffer size limit

我正在尝试 运行 mongo 来自 java 的数据库聚合查询,但缓冲区大小超过 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)
    }

我有 10 万条记录,其中 30 个字段。当我查询所有 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 不适合我的标准。因为我需要在一个请求中检索所有文档而不是一个文档。

当运行聚合时你可以告诉mongo到return一个游标。使用 3.0 Java 驱动程序中的新 APIs 看起来像这样:

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

您可能还需要告诉它在服务器上使用磁盘 space 而不是在内存中完成所有操作:

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

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

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

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

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

有关更多信息,请参阅 and