【发布时间】:2021-07-21 02:45:29
【问题描述】:
我想将下面给出的代码转换为 java 代码:
db.cabinetStatusInfo.aggregate([
{
$project: {
"fullPowerCount": 1,
"cabinCount": 1,
"fullPowerWarn": 1,
"cabinetId": 1,
"difference": {
$cond: {
if : { $eq: ["$cabinCount", null] },
then: true,
else : {
$gte: [
{ $multiply: [
{ $toInt: { $ifNull: [ "$fullPowerCount", 0 ] } }, 5
] }
,
{ $toInt: "$cabinCount" }
]
}
}
}
}
},
{
$match: {
difference: true
}
}
]);
我不知道如何使用$multiply。 Spring data 的 MongoDB 文档展示了如何将一个字段与一个数字相乘,但没有展示如何让表达式与一个数字相乘。你能帮帮我吗?
Aggregation aggregation = newAggregation(project("fullPowerCount", "cabinCount")
.and("difference")
.applyCondition(ConditionalOperators.Cond.newBuilder()
.when(Criteria.where("cabinetCount").is(null))
.then(true)
.otherwise(
//how to use $multiply,help me
(toInt(ifNull("fullPowerCount").then('0'))) )
),
match(Criteria.where("diffrence").is(true))
);
非常感谢!
【问题讨论】:
标签: mongodb aggregation-framework aggregate spring-data-mongodb