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();
相关推荐
编程老船长7 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
fishmemory7sec17 小时前
Koa2项目实战2(路由管理、项目结构优化)
数据库·mongodb·koa
缘友一世1 天前
macos安装mongodb
数据库·mongodb·macos
攻城狮numberOne1 天前
MongoDB集群模式详解及应用实战
数据库·mongodb
姜豆豆耶1 天前
MongoDB-aggregate流式计算:带条件的关联查询使用案例分析
数据库·mongodb
DngYT1 天前
MongoDB入门:安装及环境变量配置
数据库·mongodb
CoderJia程序员甲2 天前
重学SpringBoot3-集成Redis(一)
java·redis·缓存·springboot
bo_bo0012 天前
MongoDB的使用
数据库·mongodb
卡布达ovo2 天前
MongoDB入门
数据库·mongodb
fishmemory7sec2 天前
Koa2+mongodb项目实战1(项目搭建)
数据库·mongodb·koa