monogoTemplate 将 JSON{key1: 1, key2:2, key3:[{},{}{}]} 中key3 中的数组元素提取出来作为新的文档

问题描述

monogoTemplate 将 JSON{key1: 1, key2:2, key3:[{},{}{}]} 中key3 中的数组元素提取出来作为新的文档

java 复制代码
// 创建聚合操作
Aggregation agg = Aggregation.newAggregation(
    Aggregation.match(Criteria.where("YOUR_FILTER_FIELD").is(YOUR_VALUE)), // 过滤条件
    Aggregation.unwind("key3"), // 展开key3数组
    Aggregation.replaceRoot("$key3") // 设置key3数组中的每个对象为新的文档根
);
// 执行聚合查询
AggregationResults<Class_Name> results = mongoTemplate.aggregate(agg, "COLLECTION_NAME", Class_Name.class);
// 获取查询结果
List<Class_Name> extractedDocuments = results.getMappedResults();

将key3 中的数组元素提取出来作为新的文档后继续筛选

JSON{key1: 1, key2:2, key3:[{createTime: 13位时间戳},{}{}]}

java 复制代码
 Aggregation aggregation;
 List<AggregationOperation> operations = new ArrayList<>();
 operations.add(Aggregation.unwind("key3"));
 // 注意下面这条语句
 operations.add(Aggregation.replaceRoot("key3"));
 if (qo.getStartTime() != null) {
 	 // 此时key3已经成为新的文档的根字段,注意这个查询条件直接用createTime,而不是key3.createTime
     operations.add(Aggregation.match(Criteria.where("createTime").gte(qo.getStartTime())));
 }
 if (qo.getEndTime() != null) {
     operations.add(Aggregation.match(Criteria.where("createTime").lte(qo.getEndTime())));
 }

增加分页

java 复制代码
int pageNo = 1; // 查询第一页
int pageSize = 10; // 假设每页显示10个文档
Aggregation agg = Aggregation.newAggregation(
    Aggregation.match(Criteria.where("YOUR_FILTER_FIELD").is(YOUR_VALUE)), // 过滤条件
    Aggregation.unwind("key3"), // 展开key3数组
    Aggregation.replaceRoot("$key3"), // 设置key3数组中的每个对象为新的文档根
    Aggregation.skip((pageNo - 1) * pageSize), // 跳过之前页面的文档
    Aggregation.limit(pageSize) // 限制本页面返回的文档数量
);
// 执行聚合查询
AggregationResults<Class_Name> results = mongoTemplate.aggregate(agg, "COLLECTION_NAME", Class_Name.class);
// 获取分页后的查询结果
List<Class_Name> pagedDocuments = results.getMappedResults();

动态添加查询条件

java 复制代码
List<AggregationOperation> operations = new ArrayList<>();
// 添加基础条件
operations.add(Aggregation.match(Criteria.where("YOUR_FILTER_FIELD").is(YOUR_VALUE)));
operations.add(Aggregation.unwind("key3"));
operations.add(Aggregation.replaceRoot("$key3"));
// 根据需要添加更多的条件
if (需要添加额外的条件) {
    operations.add(Aggregation.match(额外的Criteria));
}
// 添加分页
int pageNo= 1; // 页码
int pageSize = 10; // 每页数量
operations.add(Aggregation.skip((pageNumber - 1) * pageSize));
operations.add(Aggregation.limit(pageSize));
// 通过列表创建聚合对象
Aggregation agg = Aggregation.newAggregation(operations);
// 执行聚合查询
AggregationResults<Class_Name> results = mongoTemplate.aggregate(agg, "COLLECTION_NAME", Class_Name.class);
// 获取结果
List<Class_Name> pagedDocuments = results.getMappedResults();
相关推荐
-一杯为品-16 小时前
【STM32单片机】#4 OLED调试&外部中断
stm32·单片机·mongodb
天上掉下来个程小白1 天前
Redis-12.在Java中操作Redis-Spring Data Redis使用方式-操作字符串类型的数据
java·redis·spring·springboot·苍穹外卖
柒月玖.1 天前
基于AT89C52单片机的轮胎压力监测系统
单片机·嵌入式硬件·mongodb
啞謎专家2 天前
在rockylinux9.4安装mongodb报错:缺少:libcrypto.so.10文件库
数据库·mongodb
shangxianjiao2 天前
Javaweb后端 AOP进阶 通知类型 切入点表达式 连接点
java·springboot·springcloud·aop
Theodore_10222 天前
Lambda 表达式入门教程
java·开发语言·spring boot·springboot
算家云2 天前
Ubuntu 22.04安装MongoDB:GLM4模型对话数据收集与微调教程
大数据·人工智能·mongodb·ubuntu·elasticsearch·算家云·glm4微调
一 乐2 天前
网红酒店|基于java+vue的网红酒店预定系统(源码+数据库+文档)
java·开发语言·数据库·毕业设计·论文·springboot·网红酒店预定系统
天上掉下来个程小白2 天前
Redis-04.Redis常用命令-字符串常用命令
java·数据库·redis·springboot·苍穹外卖
陈阳羽2 天前
云服务器Ubuntu安装宝塔面板MongoDB修改配置文件本地连接
服务器·mongodb·ubuntu