谁取代了mongoDB AggregateOutput?

本文探讨了MongoDB中AggregationOutput和AggregationOptions的废弃问题,介绍了如何使用AggregateIterable替代旧方法,并提供了一个实例。讲解了从AggregationOutput切换到AggregateIterable获取结果的正确步骤。
摘要由CSDN通过智能技术生成

AggregateOutput目前已经被废弃

AggregationOutput output = collection.aggregate(pipe);
 for (DBObject obj : output.results()) {
     //...
 }

之前的替换方案AggregationOptions目前也被废弃,.outputMode(AggregationOptions.OutputMode.INLINE) 也已弃用

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

那么,目前可供选择的方案是什么呢?

比如我们现在有这么个例子

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

我们选择用AggregateIterable 替换 AggregationOutput 以获得所需的输出/结果
**最终的替换代码如图所示如下**

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")));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值