当前位置: 首页 > 面试题库 >

如何在Spring-MongoDb聚合框架中使用$ cond操作

贺景山
2023-03-14
问题内容

我有一个聚合管道,其中包括一个像这样的项目:

$project: {
  start: {
    $cond: {
      if: {
        $eq: ["$start", "EARLY"]
      },
      then: "$deltastart.start",
      else: "$deltastart.end"
    }
  },...
},...

在mongo shell中可以正常工作。如何在Spring-Mongodb中使用Aggregation框架表达这一点?我见过ProjectionOperationBuilder,ExpressionProjectionOperationBuilder类型,但没有一个示例如何使用它们……有什么建议吗?


问题答案:

如果使用$cond通过$project管道支持操作员的当前Spring Data版本,则可以将其转换为(未测试):

import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.ConditionalOperators.Cond.*;
import org.springframework.data.mongodb.core.query.Criteria;

Cond condOperation = ConditionalOperators.when(Criteria.where("start").is("EARLY"))
                                    .thenValueOf("deltastart.start")
                                    .otherwise("deltastart.end");

Aggregation agg = newAggregation(project().and(condOperation).as("start"));
AggregationResults<MyClass> results = mongoTemplate.aggregate(agg, MyClass.class); 
List<MyClass> myList = results.getMappedResults();

对于$cond在聚合操作中不支持运算符的Spring-Data MongoDB版本,有一种解决方法是实现AggregationOperation接口以接收DBObject:

public class CustomProjectAggregationOperation implements AggregationOperation {
    private DBObject operation;

    public CustomProjectAggregationOperation (DBObject operation) {
        this.operation = operation;
    }

    @Override
    public DBObject toDBObject(AggregationOperationContext context) {
        return context.getMappedObject(operation);
    }
}

然后$project,在聚合管道中将操作实现为DBObject,该操作与你拥有的操作相同:

DBObject operation = (DBObject) new BasicDBObject(
    "$project", new BasicDBObject(
         "start", new BasicDBObject(
                "$cond", new Object[]{
                        new BasicDBObject(
                            "$eq", new Object[]{ "$start", "EARLY"}
                        ),
                        "$deltastart.start",
                        "$deltastart.end"
                 }
           )
     )
);

然后可以在TypeAggregation中使用它:

TypedAggregation<CustomClass> aggregation = newAggregation(CustomClass.class,
    new CustomProjectAggregationOperation(operation)
);
AggregationResults<CustomClass> result = mongoTemplate.aggregate(aggregation, CustomClass.class); 


 类似资料:
  • 我不熟悉Spring和Mongo。我正在使用Spring Batch获取一些报告。我的查询需要一些MongoItemReader不支持的聚合,所以我根据下面的stackoverflow链接扩展了该类。 如何在Spring批处理中使用MongoItemReader聚合查询 但我的聚合有点问题。我制作的聚合在mongoDB中运行良好,但无法将其转换为Spring mongo聚合。 MongoDb聚合按

  • 我有一套文件。每个文档有两个字段—“代码”和“状态”。我的mongodb集合包含以下文档: 我想按每个代码的状态查找计数。我想要的输出如下所示: 如何使用spring data mongodb实现这一点?我对mongodb很陌生。 更新我已成功编写mongodb查询。这是: 有人能帮助您在spring data mongodb中编写此查询吗?

  • 我正在评估MongoDB聚合框架在多大程度上适合我们的需求,因为我们目前运行在SQL Server之上。我很难执行特定的查询: 假设我有以下伪记录(建模为sql表中的列和mongodb集合中的完整文档) 我很难在mongodb聚合框架中找到合适的结构来执行这样的查询。我可以通过在管道中插入$match来查询单个范围的结果(即获取单个范围的结果),但我无法在单个管道查询中提取所有范围及其计数。

  • 是否可以在$match中执行OR? 我的意思是这样的:

  • MongoDB中的聚合框架有解释功能吗?我在文件里看不到。 我知道找到你就行了 但是在聚合框架中,我得到了一个错误