如何将 Mongodb 脚本转换为等同于 java spring-data mongodb 代码
How to transform the Mongodb script to be equivalent to the java spring-data mongodb code
我想将下面给出的代码转换为 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))
);
非常感谢!
首先,我正在根据您的聚合更改以下代码片段,我认为这是正确的方法(功能相同)。这是在聚合查询的 $project
阶段的 if
语句中。
这是您的代码:
"difference": {
$cond: {
if : { $eq: ["$cabinCount", null] },
if
子句更改为:
if : { $eq: [ { $type: "$cabinCount" }, "null" ] },
这是因为,尽管您的代码可以在 mongo
shell 中运行,但很难转换为 Spring 数据 MongoDB 代码(需要更多代码和很复杂)。请注意 MongoDB 数据中的 null
不同于 Java 中的 null
- 没有直接转换。不能指定 Null,因为它在 Java.
中
转换后的代码:
ConditionalOperators.Cond conditon =
ConditionalOperators.Cond.newBuilder()
.when(
ComparisonOperators.Eq.valueOf(
DataTypeOperators.Type.typeOf("cabinCount"))
.equalToValue("null")
)
.then(Boolean.TRUE)
.otherwise(
ComparisonOperators.Gte
.valueOf(
ArithmeticOperators.Multiply.valueOf(
ConvertOperators.Convert.convertValueOf(
ConditionalOperators.IfNull.ifNull("fullPowerCount").then(Integer.valueOf(0))
).to("int")
)
.multiplyBy(Integer.valueOf(5))
)
.greaterThanEqualTo(
ConvertOperators.Convert.convertValueOf("cabinCount").to("int")
)
);
Aggregation agg = newAggregation(
Aggregation.project("cabinCount", "fullPowerCount")
.and(
condition
)
.as("difference")
);
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "testDB");
AggregationResults<Document> results = mongoOps.aggregate(agg, "testColl", Document.class);
我想将下面给出的代码转换为 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))
);
非常感谢!
首先,我正在根据您的聚合更改以下代码片段,我认为这是正确的方法(功能相同)。这是在聚合查询的 $project
阶段的 if
语句中。
这是您的代码:
"difference": {
$cond: {
if : { $eq: ["$cabinCount", null] },
if
子句更改为:
if : { $eq: [ { $type: "$cabinCount" }, "null" ] },
这是因为,尽管您的代码可以在 mongo
shell 中运行,但很难转换为 Spring 数据 MongoDB 代码(需要更多代码和很复杂)。请注意 MongoDB 数据中的 null
不同于 Java 中的 null
- 没有直接转换。不能指定 Null,因为它在 Java.
转换后的代码:
ConditionalOperators.Cond conditon =
ConditionalOperators.Cond.newBuilder()
.when(
ComparisonOperators.Eq.valueOf(
DataTypeOperators.Type.typeOf("cabinCount"))
.equalToValue("null")
)
.then(Boolean.TRUE)
.otherwise(
ComparisonOperators.Gte
.valueOf(
ArithmeticOperators.Multiply.valueOf(
ConvertOperators.Convert.convertValueOf(
ConditionalOperators.IfNull.ifNull("fullPowerCount").then(Integer.valueOf(0))
).to("int")
)
.multiplyBy(Integer.valueOf(5))
)
.greaterThanEqualTo(
ConvertOperators.Convert.convertValueOf("cabinCount").to("int")
)
);
Aggregation agg = newAggregation(
Aggregation.project("cabinCount", "fullPowerCount")
.and(
condition
)
.as("difference")
);
MongoOperations mongoOps = new MongoTemplate(MongoClients.create(), "testDB");
AggregationResults<Document> results = mongoOps.aggregate(agg, "testColl", Document.class);