【问题标题】:How to transform the Mongodb script to be equivalent to the java spring-data mongodb code如何将mongodb脚本转换为java spring-data mongodb代码
【发布时间】: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


    【解决方案1】:

    首先,我正在从您的聚合中更改以下代码 sn-p 并且我认为是正确的方法(功能相同)。这是在聚合查询的$project 阶段的if 语句中。

    这是你的代码:

        "difference": {
            $cond: {
                if : { $eq: ["$cabinCount", null] },
    

    if 子句改为:

    if : { $eq: [ { $type: "$cabinCount" }, "null" ] },
    

    这是因为,尽管您的代码可以在 mongo shell 中运行,但很难转换为 Spring Data MongoDB 代码(需要更多代码并且很复杂)。注意 MongoDB 数据中的 null 与 Java 中的 null 不同 - 没有直接转换。不能像在 Java 中那样指定 Null。

    转换后的代码:

    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);
    

    【讨论】:

    • 代码在 Spring Boot 2.4.8 和 MongoDB v4.2.8 上运行良好。
    猜你喜欢
    • 2020-10-14
    • 2015-10-09
    • 2019-11-27
    • 2018-01-08
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多