如何在spring中使用ifNull运算符和GroupOperation

问题描述 投票:0回答:1

我想写一个查询哪个组由parentId,如果parentId为null然后分组由productId.I写了一个mongodb查询,它使用$ifNull运算符在$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}
    }
)

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

GroupOperation grpOperation = Aggregation.group("parentId")

但是我怎么能在qazxsw poi条件下写qazxsw poi条件呢?

ifNull
java spring mongodb spring-data-mongodb aggregation
1个回答
0
投票

希望这是你正在寻找的。

group
© www.soinside.com 2019 - 2024. All rights reserved.