所有方法都已被弃用,但我已经能够找到替换类以便使用java驱动程序进行聚合。
答案 0 :(得分:3)
感谢evanchooly回答,这是做什么的 替换:
AggregationOutput output = collection.aggregate(pipe);
for (DBObject obj : output.results()) {
//...
}
通过
// this build of options is taken from the original method
// aggregate(pipe) to have same behaviour
AggregationOptions options = AggregationOptions.builder()
.outputMode(AggregationOptions.OutputMode.INLINE)
.build();
Cursor cursor = collection.aggregate(pipe,options);
while (cursor.hasNext() ) {
DBObject obj = cursor.next();
//...
}
答案 1 :(得分:1)
如果您查看DBCollection,您会看到aggregate()
的多个变体,包括那些返回AggregationOutput
的变种。但是,首选版本是使用AggregationOptions
并返回Cursor
的变体。这允许您配置一些options,但最重要的是,能够在聚合结果将破坏16M文档大小限制的情况下使用游标进行响应。如果您在管道中使用$ out,这也是首选方法,因为它可以让您立即开始迭代刚刚填充的集合。
答案 2 :(得分:1)
我也面临着同样的问题,在扫描MongoAPI文档后,我感到更加困惑,但是在经历了各种站点和文档并进行了一些实验之后,找到了AggregationOutput类的替代方法。
初始代码
List<DBObject> studentAggregationQuery = new ArrayList<>();
studentAggregationQuery.add(new BasicDBObject("$roll", roll));
studentAggregationQuery.add(new BasicDBObject("$marks", marks));
AggregationOutput studentOutput = collection.aggregate(studentAggregationQuery);
List<StudentData> studentDataList = new ArrayList<>();
studentOutput.results().forEach(dbObject -> {
orderMetricsList.add(new
StudentData(mongoTemplate.getConverter().read(Students.class,
(Integer) dbObject.get("roll"), (Double) dbObject.get("marks")));
});
因此不推荐使用DBObject类,我不得不将其替换为Document类。您可以详细了解here。而且我已经将AggregateIterable与MongoCursor一起使用了,就可以了。
最终密码
List<Document> studentAggregationQuery = new ArrayList<>();
studentAggregationQuery.add(new Document("$roll", roll));
studentAggregationQuery.add(new Document("$marks", marks));
AggregateIterable studentDataIterator =
collection.aggregate(studentAggregationQuery);
List<StudentData> studentDataList = new ArrayList<>();
MongoCursor<Document> studentIterator = studentDataIterator.iterator();
while (studentIterator.hasNext()) {
Document next = studentIterator.next();
studentDataList.add(new
StudentData(mongoTemplate.getConverter().read(Students.class,
(Integer) next.get("roll"), (Double) next.get("marks")));
}
我希望您能够用AggregateIterable替换AggregationOutput以获得所需的输出/结果。
答案 3 :(得分:0)
MongoAPI Docs 不知道哪个类替换了AggregationOutput。
但是从功能上看,AggregationOutput
类用于存储mongo查询的结果集。 aggregationOutput.results()
的对象将提供一组文档,您可以使用迭代器进行迭代以获取单个文档。
所以DBCursor
就是那种情况下的近似匹配。
答案 4 :(得分:0)
这是您必须如何使用aggregata的示例。我的Java函数
/**
* MongoDB Code:
* db.review_Rating.aggregate([
* { $match : { media_id:"456" } },
* { $group:{ _id:"$media_id", avg_rating:{ $avg : "$rating" } } } ] )
*
* @param mediaID that is being sought
* @return Float arage rating for the media with param mediaID,
* if not found return null
*/
Float getAvrageRating(String mediaID) {
DBCollection collection = getConnection("MongoMedia", "review_Rating");
if(mediaID != null) {
LinkedList<DBObject> pipe = new LinkedList<>();
pipe.add(new BasicDBObject("media_id", mediaID));
DBObject groupFields = new BasicDBObject("media_id", mediaID);
groupFields.put("avg_rating", new BasicDBObject("$avg", "$rating"));
pipe.add(new BasicDBObject("$group", groupFields));
AggregationOutput cursor = collection.aggregate(pipe);
LinkedList<String> values = new LinkedList<>();
for (DBObject doc : cursor.results()) {
JSONObject obj = new JSONObject(doc.toString());
values.add(obj.getString("avg_rating"));
JSONObject idObj = (JSONObject) obj.get("_id");
values.add((String) idObj.get("$oid"));
}
return Float.valueOf(values.getFirst());
}
return null;
}