Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java/Grails-MongoDB聚合16MB缓冲区大小限制_Java_Mongodb_Grails_Aggregation Framework_Mongodb Java - Fatal编程技术网

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

Java/Grails-MongoDB聚合16MB缓冲区大小限制,java,mongodb,grails,aggregation-framework,mongodb-java,Java,Mongodb,Grails,Aggregation Framework,Mongodb Java,我试图从java运行mongo db聚合查询,但缓冲区大小超过了16MB。是否有任何方法可以调整缓冲区大小或任何其他解决方法。我没有在mongo服务器端创建集合的选项,而且我的客户端系统中也没有任何mongo实用程序,如mongo.exe或mongoExport.exe 下面是代码的一小部分 if (!datasetObject?.isFlat && jsonFor != 'collection-grid'){ //mongoPipeline = new Aggregate

我试图从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)
    }
我有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返回光标。3.0 Java驱动程序中的新API如下所示:

// Assuming MongoCollection
dataSetCollection.aggregate(pipeline).useCursor(true)
// Assuming DBCollection
dataSetCollection.aggregate(pipeline, AggregationOptions.builder()
    .allowDiskUse(true)
        .useCursor(true)
        .build())
    .useCursor(true)
您可能还需要告诉它使用服务器上的磁盘空间,而不是在内存中执行所有操作:

// Assuming MongoCollection
dataSetCollection.aggregate(pipeline).useCursor(true).allowDiskUse(true)
如果您使用的是旧驱动程序(或新驱动程序中的旧API),则这两个选项如下所示:

// Assuming MongoCollection
dataSetCollection.aggregate(pipeline).useCursor(true)
// 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();
    }
 }
}
二,。使用
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();
    }
}
公共类JavaAggregation{
公共静态void main(字符串args[])引发UnknownHostException{
MongoClient mongo=新的MongoClient();
DB=mongo.getDB(“数据库名”);
DBCollection coll=db.getCollection(“数据集”);
/*
MONGO SHELL:
db.dataset.aggregate([
{“$match”:{isFlat:true}},
{“$out”:“datasetemp”}
])
*/
DBObject match=newbasicdbobject($match),newbasicdbobject(“isFlat”,true));
def dbObjArray=新的基本对象[1]
dbObjArray[0]=匹配
List flatpipline=Arrays.asList(dbObjArray)
AggregationOptions AggregationOptions=AggregationOptions.builder()
.批量大小(100)
.outputMode(聚合选项.outputMode.CURSOR)
.allowDiskUse(真)
.build();
def cursor=dataSetCollection.aggregate(扁平管道、聚合选项)
试一试{
while(cursor.hasNext()){
System.out.println(cursor.next());
}
} 
最后{
cursor.close();
}
}

有关详细信息,请参见和

哦,没有。如果不共享您当前尝试的代码,也没有机会获得更好方法的建议。@BlakesSeven请参见更新的问题我保留了一些代码。我使用的是mongodb-win32-i386-3.0.6它不起作用。我尝试使用此dataSetCollection.aggregate(pipeline.useCursor)(true).allowDiskUse(true)。即使对于较小的数据<16mbn,它现在也会给出相同的错误。方法:com.mongodb.AggregationOutput.useCursor()的签名不适用于参数类型:(java.lang.Boolean)value:[true]@EvanChooly然后尝试第二种形式。听起来您使用的是较旧的API。抱歉,您正在谈论的是哪种API