MongoDB/Java—如何在Java中为MongoDB实现$out

MongoDB/Java—如何在Java中为MongoDB实现$out,java,mongodb,aggregation-framework,Java,Mongodb,Aggregation Framework,我有一个aggregate()查询,结果数据超过16Mb。为了处理16Mb问题,他们提供了如下内容 {$out : "datasetTemp"} // datasetTemp name of collection. 我已经试着在MongoDB shell中跟踪它的工作 db.dataset.aggregate([ { $match : {isFlat : true}}, {$out : "datasetTemp"}]) 但我需要使用管道使用JavaMongoDB来实现这一点 这是原始代码的

我有一个aggregate()查询,结果数据超过16Mb。为了处理16Mb问题,他们提供了如下内容

{$out : "datasetTemp"}
 // datasetTemp name of collection.
我已经试着在MongoDB shell中跟踪它的工作

db.dataset.aggregate([ { $match : {isFlat : true}}, {$out : "datasetTemp"}])
但我需要使用管道使用JavaMongoDB来实现这一点

这是原始代码的一部分

dbObjArray = new BasicDBObject[2]
dbObjArray[0]= cruxLevel
dbObjArray[1] = project
//dbObjArray[2] = out
List<DBObject> pipeline = Arrays.asList({dbObjArray})
output= dataset.aggregate(pipeline)
在这种情况下如何使用$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();
        }
    }
}

您可能想尝试以下方法:

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

我只是举了一个例子,这个例子并不完全是我想要的查询。我只是想说明$out的用途。我想知道如何在API ie java中使用mongodb。我只是举了一个例子,这个例子并不完全是我想要的查询。我只是想说明$out的用途。我想知道如何在API ie java中使用mongodb。
com.mongodb.CommandFailureException: { "serverUsed" : "127.0.0.1:15847" , "errmsg" : "exception: aggregation result exceeds maximum document size (16MB)" , "code" : 16389 , "ok" : 0.0}
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();
        }
    }
}