mongoTemplate的多表关联查询
一、使用场景
购物车表-car
购物车中的商品表
需要在查询car表的时候根据carId带出下面关联的商品
二、代码实现
pojo
@Data
@Document(collection = "car")
@AllArgsConstructor
@NoArgsConstructor
public class Car implements Serializable {@Idprivate String id;private String shopName;private Integer wxUserId;private Integer sellerId;private Integer sellerWxId;private Long createTime;private Long updateTime;private List<CarOid> oid;// 注意这...
}
查询方法:
public List<Car> findAll(Integer wxUserId) {Criteria criteria = Criteria.where("wxUserId").is(wxUserId);Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteria),/*关联oid 参数1-子集合的名称,参数2-父表id,参数3-字表关联父表的外键,参数4-查询子结合后给的别名as*/Aggregation.lookup("car_oid", "_id", "carId", "oid"),/*排序*/Aggregation.sort(Sort.by(Sort.Order.desc("createTime"))));Aggregation.newAggregation(Aggregation.match(criteria),lookupOperation);List<Car> cars = mongoTemplate.aggregate(aggregation, "car",Car.class).getMappedResults();return cars;}
三、测试结果
@Testpublic void find(){List<Car> carList = carService.findAllPage(7);System.out.println(carList);}// 输出 [Car(id=1540320098076995584, shopName=小爱店铺, wxUserId=7, sellerId=30, sellerWxId=6, createTime=1656075903, updateTime=1656083373, oid=[CarOid(id=1540328132346003456, carId=1540320098076995584, wxUserId=7, sellerId=31, sellerWxId=6, createTime=1656077819, updateTime=1656077847, skuId=65, title=【赠充电器】HUAWEI/华为 P50 HarmonyOS2原色双影像单元新款华为智能手机新款华为官方旗舰店p50pro, sku1Name=null, sku2Name=null, sku1Value=雪域白, sku2Value=8+128GB, pic=CNVho38U5PSz248811149531a662b07b42fd6b26edd6.png, num=8), CarOid(id=1540351428735246336, carId=1540320098076995584, wxUserId=7, sellerId=30, sellerWxId=6, createTime=1656083373, updateTime=1656083373, skuId=66, title=【赠充电器】HUAWEI/华为 P50 HarmonyOS2原色双影像单元新款华为智能手机新款华为官方旗舰店p50pro, sku1Name=null, sku2Name=null, sku1Value=雪域白, sku2Value=8+256GB, pic=CNVho38U5PSz248811149531a662b07b42fd6b26edd6.png, num=4)])]
mongoTemplate的多表关联查询相关推荐
- java mongodb 多表关联查询,多条件查询,分页,排序
前言: 由于最近项目赶,版本迭代快,不知道大BOSS从哪里听别人说MongoDB用来做关系型数据库好,而且速度快,性能高:听到这话的我,立马就反驳了回去:"MongoDB不支持事物" ...
- 解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题)
解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题) 参考文章: (1)解决Springboot+JPA中多表关联查询会查询多次的问题(n+1查询问题) (2)https: ...
- asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
本文我们将继续分享介绍Spring Boot在整合Mybatis开发企业级应用时其他典型的业务场景,即Mybatis是如何实现多表关联查询时将查询结果集与对象进行映射的,主要的内容包含"一对 ...
- jpa多表关联查询_JPA【关联查询篇】
摘要:本文主要介绍JPA的多表关联查询(一对一.一对多.双向关联.多对一.多对多)以及N+1查询的优化. 1. JPA多表关联查询 多表关联查询就是实现使用一个实体类对象操作或者查询多个表的数据. 配 ...
- oracle多表关联查询报表,oracle多表关联查询和子查询
oracle多表关联查询和子查询 一.多表关联查询 例子: sql> create table student1 ( sid varchar(3), sname varchar(6), sage ...
- 面试官:为什么mysql不建议执行超过3表以上的多表关联查询?
概述 前段时间在跟其他公司DBA交流时谈到了mysql跟PG之间在多表关联查询上的一些区别,相比之下mysql只有一种表连接类型:嵌套循环连接(nested-loop),不支持排序-合并连接(sort ...
- Mybatis-plus多表关联查询,多表分页查询
学习plus真的觉得写代码真的越来越舒服了.昨天开始接触吧,只要学会了多表关联查询.plus就能随意搭配使用了. 关于怎么搭建的就自行了去研究了哦.这里直接进入主题. 我用的是springboot+m ...
- mysql join on 索引_连接查询,表关联查询join on,索引,触发器,视图
一.连接查询 1.统计每一个部门的人数 "部门名,部门的人数" select department,count(eid) from employee group by depar ...
- 【SQL】在SQL Server中多表关联查询问题
好久没有写SQL语句的多表连接查询,总在用框架进行持久化操作.今天写了一个多表关联查询,想根据两个字段唯一确定一条数据 失败的案例如下: SELECT cyb.id,ad.name FROM [Gen ...
最新文章
- 天龙源码分析 - 选择角色流程
- Java面试笔试题大汇总一(最全+详细答案)
- phone clone android,Phone Clone
- mitmdump 脚本使用python第三方包方法(报错:in script xxx.py: No module named ‘xxx‘)
- 刚安装了Fedora 33,尝尝鲜~,哈哈~~~
- 错误 C2664 “int WideCharToMultiByte......”: 无法将参数 3 从“CString”转换为“LPCWCH” 的问题解决
- OpenGL渲染水water
- SessionFactory 概述
- IOS开发学习记录第2天之熟悉Xcode常用快捷键
- 循环队列及C语言实现二
- 淘宝H5移动端解决方案
- hadoop服务器系统设置win10,win10系统hadoop安装配置的设置技巧
- 阿里影业出品影片《1917》斩获奥斯卡多项大奖 有望国内院线上映
- python机器学习乳腺癌细胞模型
- oracle定时清理回收站,电脑设置定时清理回收站的操作方法
- 使用sysprep.exe遇到windows无法完成系统配置。若要尝试恢复配置,请重新启动计算机
- ORA-12547: TNS:lost contact 问题处理
- Visual Studio2012安装失败
- ES数据架构与关系数据库Mysql对比,例如mysql库对应es索引
- MySQL重启、启动、停止方式