java操作mongodb之多表联查的实现($lookup)
作者:要么出众,要么出局 时间:2023-08-08 10:24:07
最近在开发的过程中,一个列表的查询,涉及到了多表的关联查询,由于持久层使用的是mongodb,对这个非关系型数据使用的不是很多,所以在实现此功能的过程中出现了不少问题,现在此做记录,一为加深自己的理解,以后遇到此类问题可以快速的解决,二为遇到同样问题的小伙伴提供一点小小的帮助。
全文分为两部分:
使用robo3t编写多表关系的查询语句
将编写的查询语句整合到java项目
多表联查的查询语句:
此处使用的为mongodb的robo3t可视化工具,先说下需求:从A(假如说是日志表)表中查询出符合条件的数据,根据A表中符合条件数据查询B(假如说是信息表)表中的数据,此处也可以将B表的查询条件加入进来(类型于关系型数据库中的临时表)
mongo查询语句:
db.getCollection('A').aggregate([
{
$lookup:{
from:'B',
localField:'userid',
foreignField:'userid',
as:'userinfo'
}
},
{
$unwind:'$userrole'//把一个数组展成多个,就比如说按多表连查的userrole数组中有10数据,那么用$unwind将把一条带数组的数据分成10条,这10条数据除了userrole不同之外,其它数据都是相同的,就类似于一个展开操作
},
{
$match:{'username':'zhangsan'}
},
{
$group:{
_id:{
userid:'$userid',//这个属性必须是要A表中有的
userrole:'$userrole.roleid',//A表中有一个集合,里面存放的对象有一个名为roleid的属性
},
operateTime:{
$last:'$operateTime'//取A表操作时间最后一条件数
}
info:{
$first:'$userinfo'//因为数组的扩展,造成了大量的重复数据(只有userrole不同),$first是只取最新的一条
}
}
},
{
$sort:{'operateTime':-1}//操作时间倒序,-1:倒序,1:升序
},
{
$skip:0//跳过几条数据,也就是从第几条数据开始取
},
{
$limit:5//每页显示几条数据
}
]);
java代码整合查询语句
//定义分组字段
String[] groupIds = new String[] {"$userid","$userrole.roleid"};
//定义查询条件
Criteria criteria = new Criteria();
//相当于where username = "zhangsan"
criteria.and("username").is("zhangsan");
//相当于 where age not in("15","20")
criteria.and("age").nin("15","20");
//in操作对应的语句
//criteria.and("").in();
//定义排序条件
Sort sort = new Sort(Direction.DESC,"operateTime");
//联合查询总条数,分页用
Aggregation aggregationCount = Aggregation.newAggregation(
Aggregation.match(criteria);//查询条件
Aggregation.group(groupIds);//分组字段
);
//联合查询条件
Aggregation newAggregation = Aggregation.newAggregation(
Aggregation.lookup('B','userid','userid','userinfo'),//从表名,主表联接字段,从表联接字段,别名
Aggregation.unwind("$userrole"),
Aggregation.match(criteria),
Aggregation.group(groupIds)
.last("$operateTime").as("operateTime")//取值,起别名
.first("$userinfo").as("info"),
Aggregation.sort(sort),
Aggregation.skip(pageSize*(pageNumber-1L)),//Long类型的参数
Aggregation.limit(pageSize)
);
//查询
AggregationResults<BasicDBObject> aggregate = mongoTemplate.aggregate(
newAggregation ,"A",BasicDBObject.class//A表,是查询的主表
);
int count = mongoTemplate.aggregate(aggregationCount ,"A",BasicDBObject.class).getMappedResults().size();
//组装分页对象
Page<BasicDBObject> pager = new Page<>(aggregate.getMappedResults(),count,pageSize,pageNumber,page*(pageNumber-1));
//对象转换
将BasicDBObject转换成前面需要的类型.....
来源:https://blog.csdn.net/zhangxianling11/article/details/90371629
标签:java,mongodb,多表联查
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Idea自动生成Entity实现过程详解
2022-06-03 12:18:24
![](https://img.aspxhome.com/file/2023/4/89474_0s.png)
Java Swing实现窗体添加背景图片的2种方法详解
2021-10-26 19:01:18
![](https://img.aspxhome.com/file/2023/1/62231_0s.jpg)
Java详解表格的创建与使用流程
2021-06-19 22:58:52
![](https://img.aspxhome.com/file/2023/1/108421_0s.png)
spring与disruptor集成的简单示例
2021-12-16 11:01:41
Java事件处理机制和适配器全面解析
2021-10-23 04:19:32
![](https://img.aspxhome.com/file/2023/0/73070_0s.png)
C#使用反射机制实现延迟绑定
2021-06-13 22:22:42
java代码规范review异常事故记录
2023-08-03 07:23:24
![](https://img.aspxhome.com/file/2023/5/110805_0s.jpg)
java(包括springboot)读取resources下文件方式实现
2021-06-03 20:16:06
![](https://img.aspxhome.com/file/2023/4/68184_0s.png)
Android控件之ListView用法实例详解
2023-09-11 00:39:26
![](https://img.aspxhome.com/file/2023/0/90130_0s.png)
C#函数式编程中的部分应用详解
2023-12-01 23:02:44
![](https://img.aspxhome.com/file/2023/7/126127_0s.png)
C# Winform自动更新程序实例详解
2021-12-06 05:52:57
解决@Transactional注解事务不回滚不起作用的问题
2022-10-29 09:18:51
SpringBoot项目多数据源及mybatis 驼峰失效的问题解决方法
2023-07-25 07:09:08
浅谈Java序列化和hessian序列化的差异
2022-11-17 07:46:13
Gson之toJson和fromJson方法的具体使用
2021-07-20 16:28:47
![](https://img.aspxhome.com/file/2023/2/75512_0s.png)
C# DateTime.Compare()方法案例详解
2023-08-12 23:41:36
Java如何设置PDF文档背景色详解
2022-07-08 08:17:19
![](https://img.aspxhome.com/file/2023/7/77947_0s.png)
Shiro 控制并发登录人数限制及登录踢出的实现代码
2023-09-18 02:03:56
![](https://img.aspxhome.com/file/2023/5/68515_0s.jpg)
Android实现带圆环的圆形头像
2021-09-30 09:04:47
![](https://img.aspxhome.com/file/2023/4/92584_0s.jpg)
关于Java中增强for循环使用的注意事项
2021-08-09 16:47:43
![](https://img.aspxhome.com/file/2023/7/63307_0s.png)