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();
相关推荐
jiugie7 小时前
MongoDB学习
数据库·python·mongodb
hzulwy7 小时前
MongoDB应用设计调优
数据库·mongodb
Hanson Huang1 天前
【存储中间件API】MySQL、Redis、MongoDB、ES常见api操作及性能比较
redis·mysql·mongodb·es
落落落sss1 天前
MongoDB
数据库·windows·redis·mongodb·微服务·wpf
wolf犭良1 天前
19、《Springboot+MongoDB整合:玩转文档型数据库》
数据库·spring boot·mongodb
人类群星闪耀时1 天前
深入理解 NoSQL 数据库:MongoDB 与 Cassandra
数据库·mongodb·nosql
LUCIAZZZ1 天前
SkyWalking快速入门
java·后端·spring·spring cloud·微服务·springboot·skywalking
ccmjga3 天前
升级 SpringBoot3 全项目讲解 — Spring Boot 3 中如何发Http请求?
java·spring boot·后端·docker·gradle·springboot·spring security
一张假钞3 天前
MongoDB between ... and ... 操作
数据库·mongodb
奔跑吧邓邓子3 天前
【Python爬虫(23)】探秘Python爬虫数据存储:MongoDB实战指南
开发语言·爬虫·python·mongodb·实战