MongoTemplate Aggregation Switch

  1. 需求: 统计不同年龄段的人(年龄段不定)
public ResponseEntity query(@PathVariable("db") String dbName) {
        List<AggregationOperation> operations = new ArrayList<>();
        Fields fields = Fields.fields("info_age");
        operations.add(Aggregation.project(fields).and(ConditionalOperators.switchCases(
                // 这个switch可以有多组, 此处参数是个数组
                ConditionalOperators.Switch.CaseOperator.when(
                        BooleanOperators.And.and(
                                ComparisonOperators.Gte.valueOf("info_age").greaterThanEqualToValue(0),
                                ComparisonOperators.Lte.valueOf("info_age").lessThanEqualToValue(30)
                        )
                ).then("0-30岁"),

                ConditionalOperators.Switch.CaseOperator.when(
                        BooleanOperators.And.and(
                                ComparisonOperators.Gt.valueOf("info_age").greaterThanValue(30),
                                ComparisonOperators.Lte.valueOf("info_age").lessThanEqualToValue(60)
                        )
                ).then("30-60岁"),

                ConditionalOperators.Switch.CaseOperator.when(
                        BooleanOperators.And.and(
                                ComparisonOperators.Gt.valueOf("info_age").greaterThanValue(60),
                                ComparisonOperators.Lte.valueOf("info_age").lessThanEqualToValue(100)
                        )
                ).then("60-100岁")
        ).defaultTo("不符合的数据")).as("age"));
        operations.add(Aggregation.group("$age").count().as("total"));
        Aggregation aggregation = Aggregation.newAggregation(operations);
        AggregationResults<Map> results = mongoTemplate.aggregate(aggregation, dbName, Map.class);
        List<Map> myList = results.getMappedResults();
        // [{_id=不符合的数据, total=6960}, {_id=60-100岁, total=6782}, {_id=0-30岁, total=34731}, {_id=30-60岁, total=47972}]
        System.out.println(myList);
        return ResponseEntity.success();
    }
  1. 对应的Mongodb查询语句是
db.user.aggregate(
[
    {
        "$project": {
            "info_age": 1,
            "_id": 1,
            "age": {
                "$switch": {
                    "branches": [
                        {
                            "case": {
                                "$and": [
                                    {
                                        "$gte": [
                                            "$info_age",
                                            0
                                        ]
                                    },
                                    {
                                        "$lte": [
                                            "$info_age",
                                            30
                                        ]
                                    }
                                ]
                            },
                            "then": 1
                        },
                        {
                            "case": {
                                "$and": [
                                    {
                                        "$gt": [
                                            "$info_age",
                                            30
                                        ]
                                    },
                                    {
                                        "$lte": [
                                            "$info_age",
                                            60
                                        ]
                                    }
                                ]
                            },
                            "then": 2
                        },
                        {
                            "case": {
                                "$and": [
                                    {
                                        "$gt": [
                                            "$info_age",
                                            60
                                        ]
                                    },
                                    {
                                        "$lte": [
                                            "$info_age",
                                            100
                                        ]
                                    }
                                ]
                            },
                            "then": 3
                        }
                    ],
                    "default": "4"
                }
            }
        }
    },
    {
        "$group": {
            "_id": "$age",
            "total": {
                "$sum": 1
            }
        }
    }
]
    )
  1. 返回的结果是
[{_id=不符合的数据, total=6960}, {_id=60-100岁, total=6782}, {_id=0-30岁, total=34731}, {_id=30-60岁, total=47972}]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,如果你正在使用MongoDB数据库,并且是通过Spring Data MongoDB中的MongoTemplate进行操作的话,那么可以使用Aggregation.group()方法对数据进行分组聚合操作。 Aggregation.group()方法接受一个或多个字段作为参数,并将这些字段作为分组依据。例如,如果你想按照某个字段进行分组,可以这样写: ``` Aggregation.group("fieldName"); ``` 如果你想对某个字段进行特殊处理,可以使用Aggregation操作符,例如: 1. $sum:求和 ``` Aggregation.group("fieldName").sum("otherFieldName").as("total"); ``` 这个操作会对"otherFieldName"字段进行求和,并将结果保存在名为"total"的新字段中。 2. $avg:求平均值 ``` Aggregation.group("fieldName").avg("otherFieldName").as("average"); ``` 这个操作会对"otherFieldName"字段进行求平均值,并将结果保存在名为"average"的新字段中。 3. $max:求最大值 ``` Aggregation.group("fieldName").max("otherFieldName").as("maxValue"); ``` 这个操作会对"otherFieldName"字段进行求最大值,并将结果保存在名为"maxValue"的新字段中。 4. $min:求最小值 ``` Aggregation.group("fieldName").min("otherFieldName").as("minValue"); ``` 这个操作会对"otherFieldName"字段进行求最小值,并将结果保存在名为"minValue"的新字段中。 5. $push:将某个字段的值放入一个数组中 ``` Aggregation.group("fieldName").push("otherFieldName").as("fieldValues"); ``` 这个操作会将"otherFieldName"字段的值放入一个名为"fieldValues"的数组中。 6. $addToSet:将某个字段的值放入一个集合中 ``` Aggregation.group("fieldName").addToSet("otherFieldName").as("fieldValues"); ``` 这个操作会将"otherFieldName"字段的值放入一个名为"fieldValues"的集合中。 除了上述操作符之外,还有很多其他的操作符可以使用。你可以根据自己的需求进行选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值