MongoDB / Java-如何在Java中为mongodb实现$ out [英] MongoDB/Java - How implement $out in java for mongodb
本文介绍了MongoDB / Java-如何在Java中为mongodb实现$ out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个aggregate()查询,其结果数据超过16Mb。为了处理16Mb问题,他们提供了以下内容。
I have a aggregate() query which results data more than 16Mb. To work with 16Mb issue they provided something like below.
{$out : "datasetTemp"}
// datasetTemp name of collection.
我在MongoDB shell中尝试了以下操作。
I have tried following in MongoDB shell it working.
db.dataset.aggregate([ { $match : {isFlat : true}}, {$out : "datasetTemp"}])
但是我需要使用管道使用java-mongodb来实现。
But I need to do it with java-mongodb using pipeline.
这是原始代码的一部分
dbObjArray = new BasicDBObject[2]
dbObjArray[0]= cruxLevel
dbObjArray[1] = project
//dbObjArray[2] = out
List<DBObject> pipeline = Arrays.asList({dbObjArray})
output= dataset.aggregate(pipeline)
我尝试了这不起作用(无法创建集合)
I tried this which is not working (collection is not getting created)
dbObjArray = new BasicDBObject[2]
dbObjArray[0]= cruxLevel
dbObjArray[1] = project
//dbObjArray[2] = out
List<DBObject> pipeline = Arrays.asList({dbObjArray},{$out:"datasetTemp"})
output= dataset.aggregate(pipeline)
不创建集合时不会抛出任何错误。
Which is not throwing any error not creating collection.
我也尝试过此操作
DBObject out = new BasicDBObject('$out', "temp_colls");
dbObjArray = new BasicDBObject[2]
dbObjArray[0]= cruxLevel
dbObjArray[1] = project
// dbObjArray[2] = out
List<DBObject> pipeline = Arrays.asList({dbObjArray})
if (!datasetObject?.isFlat && jsonFor != 'collection-grid') {
//mongoPipeline = new AggregateArgs (Pipeline = pipeline, AllowDiskUse = true, OutputMode = AggregateOutputMode.Cursor)
output= dataSetCollection.aggregate(pipeline,out)
DBCollection tempColl = dataBase.getCollection("temp_colls")
def cursor = tempColl.find();
try {
while(cursor.hasNext()) {
System.out.println(cursor.next());
}
} finally {
cursor.close();
}
output = cursor
}else{
output= dataSetCollection.aggregate(project)
}
output.results().eachWithIndex{list,index->
dataList.add(output.results()[index])
}
将其抛出以下错误。
com.mongodb.CommandFailureException: { "serverUsed" : "127.0.0.1:15847" , "errmsg" : "exception: aggregation result exceeds maximum document size (16MB)" , "code" : 16389 , "ok" : 0.0}
在这种情况下如何使用 $ out 。
How use $out under this scenario.
谢谢。
推荐答案
您可能想尝试一下:
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();
}
}
}
这篇关于MongoDB / Java-如何在Java中为mongodb实现$ out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文