我有一个aggregate()查询,结果数据超过16Mb。为了处理16Mb问题,他们提供了类似下面的内容。
{$out : "datasetTemp"}
// datasetTemp name of collection.
我在MongoDB shell中尝试过它可以正常工作。
db.dataset.aggregate([ { $match : {isFlat : true}}, {$out : "datasetTemp"}])
但是我需要使用管道来实现java-mongodb。
这是原始代码的一部分
dbObjArray = new BasicDBObject[2]
dbObjArray[0]= cruxLevel
dbObjArray[1] = project
//dbObjArray[2] = out
List<DBObject> pipeline = Arrays.asList({dbObjArray})
output= dataset.aggregate(pipeline)
我试过这个不起作用(集合没有被创建)
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)
没有抛出任何错误而没有创建集合。
我也试过这个
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 。
谢谢。
答案 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();
}
}
}