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();
相关推荐
好吃的肘子10 小时前
MongoDB 应用实战
大数据·开发语言·数据库·算法·mongodb·全文检索
独泪了无痕11 小时前
MongoTemplate 基础使用帮助手册
spring boot·mongodb
好吃的肘子15 小时前
MongoDB入门
数据库·mongodb
柳如烟@1 天前
在Rocky Linux 9.5上部署MongoDB 8.0.9:从安装到认证的完整指南
linux·运维·mongodb
好吃的肘子1 天前
MongoDB 高可用复制集架构
数据库·mongodb·架构
码上飞扬1 天前
MongoDB数据库深度解析:架构、特性与应用场景
数据库·mongodb·架构
带刺的坐椅2 天前
SpringBoot3 使用 SolonMCP 开发 MCP
java·ai·springboot·solon·mcp
码农捻旧2 天前
解决Mongoose “Cannot overwrite model once compiled“ 错误的完整指南
javascript·数据库·mongodb·node.js·express
LUCIAZZZ2 天前
JVM之虚拟机运行
java·jvm·spring·操作系统·springboot
堕落年代2 天前
SpringSecurity当中的CSRF防范详解
前端·springboot·csrf