当前位置:   article > 正文

Java中Mongo语法使用,聚合函数(包含mongo多表关联查询)_javaaggregation语法

javaaggregation语法

使用前首先需要配置好MongoTemplate,(详细参考:MongoTemplate的使用)

配置好后在类中注入MongoTemplate对象

@Autowired
private MongoTemplate mongoTemplate;
  • 1
  • 2

常用函数

使用前我们先来了解一下常用的函数

1Aggregation.group() : 聚合函数,将某个字段或者某个数组作为分组统计的依据,在group的基础上又扩展出以下函数:

sum() : 求和
max() : 获取最大值
min() : 获取最小值
avg() : 获取平均值
count() : 统计条目数
first () : 获取group by 后的某个字段的首个值
last() : 获取 group by 后的某个字段的最后一个值
push() : 在结果文档中插入值到一个数组中
addToSet() : 在结果文档中插入值到一个数组中,但不创建副本(作为集合)2Aggregation.match() : 过滤函数,主要存储过滤数据的条件,输出符合条件的记录
3Aggregation.project(): 修改数据结构函数,将前面管道中的获取的字段进行重名,增加,修改字段等操作。

4Aggregation.unwind():将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。当preserveNullAndEmptyArrays为true时,将包括字段为null,空,或者缺失的数据;
5Aggregation.sort(): 排序函数,将上级管道的内容按照某个字段进行排序并且输出。值为1升、-1降。sort一般放在group后,也就是说得到结果后再排序,如果先排序再分组没什么意义;

6Aggregation.limit(): 限制输出函数,将聚合返回的内容限定在某个条目之内。通常作为页面大小
7Aggregation.skip(): 跳过指定数量的条目再开始返回数据的函数,通常和sort()limit()配合,实现数据翻页查询等操作。

8Aggregation.lookup(): 连表查询,将被关联集合添加到执行操作的集合中。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

实际用例

用accountId和statusgroup操作

mongodb: 
db.getCollection('mro_fn_subscribes').aggregate([
    {
        $group:{
        "_id":{"_id":"$accountId", "status": "$status" },
        "count":{"$sum": NumberInt(1)},
        "statusSum":{"$sum": "$status"},
        "codeAvg":{"$avg":"$fnCode"},
        "codeMax":{"$max":"$fnCode"},
        "codeMin":{"$min":"$fnCode"},
        "codeFirst":{"$first":"$fnCode"},
        "codeLast":{"$last":"$fnCode"},
        }
        
    }
])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
java:
Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.group("accountId", "status")
                .count().as("count")
                .sum("status").as("statusSum")
                .avg("fnCode").as("codeAvg")
                .max("fnCode").as("codeMax")
                .min("fnCode").as("codeMin")
                .first("fnCode").as("codeFirst")
                .last("fnCode").as("codeLast")
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

match管道过滤:

mongodb: 
db.getCollection('mro_fn_subscribes').aggregate(
  {$match:{userId:"a"}}
)
  • 1
  • 2
  • 3
  • 4
java:
Aggregation aggregation = Aggregation.newAggregation(
        Aggregation.match(new Criteria().and("userId").is("a")
);
  • 1
  • 2
  • 3
  • 4

project筛选字段:

mongo:
db.getCollection('mro_fn_subscribes').aggregate([
    {
        "$group" : {
            "_id" : "$_id", 
            "num" : {
                "$sum" : "$num"
            }, 
            "firstName" : {
                "$first" : "$name"
            }, 
            "lastName" : {
                "$last" : "$name"
            }
        }
    }, 
    {
        "$project" : {
            "_id" : 1, 
            "num" : 1, 
            "firstName" : 1, 
            "name" : "$lastName"
        }
    }
])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
java:
// 初始化聚合
Aggregation aggregation = Aggregation.newAggregation(
    	Aggregation.group(new String[] {"_id"}).sum("num").as("num")
	    	.first("name").as("firstName")
	    	.last("name").as("lastName"),
	Aggregation.project("_id", "num", "firstName")
                .and("lastName").as("name") // 重新命名字段
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

unwind拆分数组

mongo:
db.col.aggregate(
  {
    $match:{userid:"a"}
  }, 
  {
    $unwind:{
      path:"$items", includeArrayIndex: "arrayIndex"
    }
  }
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
java:
Aggregation aggregation = Aggregation.newAggregation(
    Aggregation.match(new Criteria().and("userId").is("a"),
    Aggregation.unwind("items",true)
);
  • 1
  • 2
  • 3
  • 4
  • 5

sort skip limit处理数据:

Mongo:
db.getCollection('mro_fn_subscribes').aggregate([
    {
        "$group" : {
             _id:{"_id":"$accountId", "status": "$status" } 
        }
    }, 
    {
        "$sort" : {
            "num" : -1
        }
    }, 
    {
        "$skip" : NumberInt(10)
    }, 
    {
        "$limit" : NumberInt(2)
    }
]
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
Java:
Aggregation aggregation = Aggregation.newAggregation(
    	Aggregation.group("accountId", "status")
	Aggregation.sort(Direction.DESC, "num"),		//将num降序
	Aggregation.skip(10),					//从第10条记录开始
	Aggregation.limit(2)					//取两条数据
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

lookup多表关联查询:

Mongo:
db.getCollection('mro_accounts').aggregate([
    {
        $lookup: {
            from:"mro_profiles",   # 被关联表名
            localField:"userName", # 主表(mro_accounts)中用于关联的字段
            foreignField:"mobile", # 被关联表(mro_profiles)中用于关联的字段
            as:"profileDoc"        # 被关联的表的别名
            }
        }
])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
Java
Aggregation aggregation = Aggregation.newAggregation(
    	Aggregation.lookup("mro_profiles", "userName", "mobile", "profileDoc") //分别对应from, localField, foreignField, as
);
  • 1
  • 2
  • 3
  • 4

获取查询结果

在创建好Aggregation对象之后,再用 mongotemplate.aggregate(aggregation, “mro_fn_subscribes”, Fnsubscribe.class).getMappedResults() 获取查询的对象列表

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/56029
推荐阅读
相关标签
  

闽ICP备14008679号