如何使用spring-data-mongodb重写这个投影查询?我遇到的主要问题是$and运算符。
查询:
{
$project: {
"_id": 1,
"totalAmount": {
$cond: [{
$and: [{
$eq: ["$type", "entity"]
}, {
$eq: ["$status", "ACTIVE"]
}]
}, "$amount", 0]
},
}
}我写了
ConditionalOperators.Cond operator = ConditionalOperators
.when(new Criteria()
.andOperator(Criteria.where("type").is("entity") , Criteria.where("status").is("ACTIVE")))
.thenValueOf("amount").otherwise(0.0);
ProjectionOperation projectionOperationNavLogs = project().and("_id").as("_id").and(operator).as("totalAmount"); 但这不是相同的查询
发布于 2019-05-20 20:21:50
$cond表达式具有以下两种语法之一:
{ $cond:{ if:,then:,else:}}
或
{ $cond:,,}
生成的查询使用更冗长的语法这一事实不是问题。
https://stackoverflow.com/questions/56210284
复制相似问题