春季如何在GroupOperation中使用ifNull运算符

时间:2019-04-11 10:07:57

标签: java spring mongodb spring-data-mongodb aggregation

我想编写一个查询,其中按"expo": { ... "splash": { "image": "./assets/images/common/splash.png", "resizeMode": "contain", "backgroundColor": "#CEECFF" }, "android": { ... "splash": { "backgroundColor": "#CEECFF", "resizeMode": "contain", "mdpi": "./assets/images/common/splash-mdpi.png", "hdpi": "./assets/images/common/splash-hdpi.png", "xhdpi": "./assets/images/common/splash-xhdpi.png", "xxhdpi": "./assets/images/common/splash-xxhdpi.png", "xxxhdpi": "./assets/images/common/splash-xxxhdpi.png" } } } 分组,如果parentId为空,则按parentId分组。我编写了一个mongodb查询,该查询在{内使用productId运算符{1}}操作符来实现这一点,这是查询。

$ifNull

我想将此查询转换为spring代码。以下代码将按字段$group进行分组,并且工作正常。

db.collection.aggregate(
    {
        $match:{
            $and : [
                {"tenant_id":1} 
                {"product_type":1}
            ]
        },
        {
            $group : {
                "_id":{ $ifNull: [ "$parent_id", "$product_id" ] },
                "data": {"$first" : "$$ROOT"}
            }
        },
        { $sort: {"data.created" : 1 } },
        {$limit: 4},
        {$skip:2}
    }
)

但是我该如何在parentId条件内写这样的GroupOperation grpOperation = Aggregation.group("parentId") 条件呢?

ifNull

1 个答案:

答案 0 :(得分:0)

希望这就是您想要的。

Aggregation aggregation = Aggregation.newAggregation(
                    group("parentId")
       .applyCondition(ConditionalOperators.ifNull("parentId").then("productId")))