SpingBoot整合MongoDB实现聚合统计,分页查询和批量更新数据

从入门到XXX,Java程序使用MongoDB作为库实现折线图和分页列表需求

  • 新建MongoDB库/集合并造测试数据
    • 1.建库
    • 2.造测试数据
    • 3.MongoDB的写入/聚合/更新语法
  • SpringBoot整合MongoDB
    • 1.依赖和配置
    • 2.通过JAVA程序操作MongoDB
  • 参考资料

新建MongoDB库/集合并造测试数据

1.建库

// 创建库
use test
// 显示库列表
show dbs

在这里插入图片描述

2.造测试数据

MongoDB插入数据数据的时候会隐式创建集合(相当于MySQL里表的)

unique_id	name	age	gender	birthdy	phoneNo	hobby	address	createDate	createBy
46d0e012a0850065f21fad2b0b374b9c	张三	32	1	1991/10/21	13188711907	跑步健身		2021/9/30 22:31	test1
9584e3ab05a39a29af6ba02c2afcb91b	里斯	21	1	1991/10/21	15678780987			2021/9/30 9:18	test1
f9b41dee82041093189abd76afaae531	王五	34	1	1991/10/21	15678780988		上海市浦东新区	2021/9/30 8:03	test1
7a5c2bb56b51d4bf39874f953faae83e	小刘	35	0	1991/10/21	13478780989			2021/9/30 0:07	test1
e4ffa1e0da328035eb9f6142d2bd3f10	七七七	18	0	1991/10/21	15166580990	测试		2021/9/30 19:14	test1
beb6c2cc3f5cd2832628969c8e78bc56	八哥	29	1	1991/10/21	18678780991	充分利用大型连锁商贸企业网络优	江苏省连云港	2021/9/30 8:09	test1
0c746d36d03b0c3612fe59ebec48a221	舅舅	38	1	1991/10/21	17678780992	抽烟喝酒		2021/9/30 8:09	test1
8bcff975dae4ba788dd28b412fec3c92	实施	39	0	1991/10/21	19678780993			2021/9/30 16:44	test1

小工具:https://www.bejson.com/json/col2json/
把上面的数据或者Excel / csv 上的数据直接转换为JSON ,如下图

SpingBoot整合MongoDB实现聚合统计,分页查询和批量更新数据_第1张图片

3.MongoDB的写入/聚合/更新语法


// 写入数据并隐式创建集合
// 1.写入单条数据
db.people.insertOne({"name":"小明","age":18,"gender":"女","birthdy":"2003/01/01"})
// 2.写入多条数据
db.people.insertMany([
                       {"unique_id":"46d0e012a0850065f21fad2b0b374b9c","name":"张三","age":32,"gender":1,"birthdy":"1991/10/21","phoneNo":13188711907,"hobby":"跑步健身","address":"","createDate":"2021/9/30 22:31","createBy":"test1"},
                       {"unique_id":"9584e3ab05a39a29af6ba02c2afcb91b","name":"里斯","age":21,"gender":1,"birthdy":"1991/10/21","phoneNo":15678780987,"hobby":"","address":"","createDate":"2021/9/30 9:18","createBy":"test1"},
                       {"unique_id":"f9b41dee82041093189abd76afaae531","name":"王五","age":34,"gender":1,"birthdy":"1991/10/21","phoneNo":15678780988,"hobby":"","address":"上海市浦东新区","createDate":"2021/9/30 8:03","createBy":"test1"},
                       {"unique_id":"7a5c2bb56b51d4bf39874f953faae83e","name":"小刘","age":35,"gender":0,"birthdy":"1991/10/21","phoneNo":13478780989,"hobby":"","address":"","createDate":"2021/9/30 0:07","createBy":"test1"},
                       {"unique_id":"e4ffa1e0da328035eb9f6142d2bd3f10","name":"七七七","age":18,"gender":0,"birthdy":"1991/10/21","phoneNo":15166580990,"hobby":"测试","address":"","createDate":"2021/9/30 19:14","createBy":"test1"},
                       {"unique_id":"beb6c2cc3f5cd2832628969c8e78bc56","name":"八哥","age":29,"gender":1,"birthdy":"1991/10/21","phoneNo":18678780991,"hobby":"充分利用大型连锁商贸企业网络优","address":"江苏省连云港","createDate":"2021/9/30 8:09","createBy":"test1"},
                       {"unique_id":"0c746d36d03b0c3612fe59ebec48a221","name":"舅舅","age":38,"gender":1,"birthdy":"1991/10/21","phoneNo":17678780992,"hobby":"抽烟喝酒","address":"","createDate":"2021/9/30 8:09","createBy":"test1"},
                       {"unique_id":"8bcff975dae4ba788dd28b412fec3c92","name":"实施","age":39,"gender":0,"birthdy":"1991/10/21","phoneNo":19678780993,"hobby":"","address":"","createDate":"2021/9/30 16:44","createBy":"test1"},
                       ])
// 3.查询集合
db.people.find()

// 4.统计时间大于XXX,性别为0的数据条数
db.people.count(
    {create_date:{$gt:'2021-10-19T08:03:32.486Z'},gender:0}
)

// 5.聚合:按天统计数据,12的话就是按小时统计
db.people.aggregate(
    [
        { $project :{ day :{$substr :["$createDate",0,10] }}},
        { $group : { _id :"$day", number : {$sum : 1}}},
        { $sort :  { _id : -1}}
    ]
);

SpringBoot整合MongoDB

1.依赖和配置

	<!-- MongoDB-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-mongodb</artifactId>
		</dependency>
		<!-- java操作db驱动 -->
		<dependency>
			<groupId>org.mongodb</groupId>
			<artifactId>mongo-java-driver</artifactId>
		</dependency>

日志打印的yml配置:

#日志打印    
logging:
  level: 
    org.springframework.data.mongodb.core: DEBUG

Java 建立数据库连接

/**创建一个客户端实例,这里是副本集的连接方式**/
MongoClient mongoClient = new MongoClient(new MongoClientURI(mongodb://${userName}:${passward}@${ip}:${port},${ip}:${port}/${数据库名}?authSource=admin&replicaSet=replica});
MongoDatabase db = mongoClient.getDatabase( ${数据库名} );
//传入集合名,取集合连接
MongoCollection<Document> collection = mongoDBClient.getCollection(people);

2.通过JAVA程序操作MongoDB

1.写入数据

Document document = new Document().append("create_date",new Date()).append("is_risk",0).append("handle_result","有风险").append("handle_status",1).append("handle_user","测试人DDD").append("handle_date",new Date());
		collection.insertOne(document);	

2.聚合查询:分组,求和,统计。统计指定时间段内新增人员的性别数据

collection.aggregate(
			Arrays.asList(
					//匹配时间段
					Aggregates.match(Filters.gte("createDate","2021-10-21 01:58:45")),
					Aggregates.match(Filters.lte("createDate","2021-10-29 01:58:45")),
					//根据gender字段分组,求和,统计在男生和女生的数量
					Aggregates.group("$gender", Accumulators.sum("count", 1))
			)
		).forEach((Block<Document>) document -> {
					String gender =  document.get("_id")+"";
					Integer count = (Integer) document.get("count");
					System.out.printf("~~~~~gender:{},count:{}",gender,count);
				}
		);

3.分页查询

public <T> PageResult<T> listQueryPage(String collectionName, Date createStart,Date createEnd,int current,int pageSize) {
		MongoCollection<Document> collection = mongoDBClient.getCollection(collectionName);
		PageResult<Map<String, Object>> page=new PageResult<>(current, pageSize);
		List<Map<String, Object>> records = Lists.newArrayList();
	
		FindIterable<Document> iterable = collection.find(
				//过滤 XXX < 日期 <= XXX
				and(gt("createDate",createStart),lte("createDate",createEnd)))
				//分页  
				.skip(current).limit(pageSize)
				//按生日排序
				.sort(new Document("birthdy", -1)).sort(new Document("createDate", -1));

		iterable.forEach((Block<Document>) doc -> {
			Map<String, Object> map = new HashMap<>(doc);
			records.add(map);
		});
		page.setRecords(records);
		return (PageResult<T>) page;
	}

参考资料

1.《MongoDB进阶与实战:微服务整合、性能优化、架构管理》: link.
2. 日期处理函数: link.

你可能感兴趣的:(mongodb,数据库,database,java)