【发布时间】:2020-09-09 01:10:39
【问题描述】:
我是 mongo 聚合的新手,我想检查在 spring mongo 中是否可以进行以下操作。我知道我们可以写出给定的条件,
ConditionalOperators.Cond cond = when(Criteria.where("status").is("PRESENT")).then(1)
.otherwise(when(Criteria.where("status").is("ABSENT")).then(2)
.otherwise(100));
鉴于我有可能的条件值的映射,我想知道是否可以使用映射将值传递给条件。
Map<String, Integer> dynamicValues = new HashMap<>();
dynamicValues.put("PRESENT", 1);
dynamicValues.put("ABSENT", 2);
这就是我现在所拥有的。但它没有按预期工作。
List<String> dynamicValues = Arrays.asList("PRESENT", "ABSENT");
ConditionalOperators.Cond.OtherwiseBuilder operator = ConditionalOperators.when(Criteria.where("status").is(dynamicValues.get(0))).then(0));
for (String s : dynamicValues) {
ConditionalOperators.Cond.OtherwiseBuilder shippingDestination = when(Criteria.where("status").is(s)).then(1);
operator.otherwise(shippingDestination);
}
return (ConditionalOperators.Cond) operator;
我已经硬编码了 then 临时值。
【问题讨论】:
-
能否提供完整的查询?
-
@Valijon,我已经在代码中添加了当前的实现。但不幸的是,它不起作用。
标签: java spring mongodb aggregation-framework