当前位置:网站首页>记录一次Mongotemplate的And和Or的各种套
记录一次Mongotemplate的And和Or的各种套
2022-06-24 12:58:00 【人形bug制造机9527】
需求是查询用户的行程安排信息
查询用户过去七天到未来的行程安排,
也就是说数据必须满足如下条件
1.见面时间 >= 当前时间-7天
2.用户id=发送者id,且发送人确认情况=0或者=null
或者用户id=接收者,且发送人确认情况=0或者=null
以上两个表示用户没有确认或者否认这个记录,所以进行展示
3.行程必须是被同意过的
4.行程必须是未见面的
5.指定用户
mysql大概的写法
select
*
from table
where
isConsent = 1
and
isMeet = 0
and
userId = xxxxxxxxx
and meetime > 当前时间减7天
and
(
userId = recId and (recConfirm = 0 or recConfirm = null)
or
userId = senId and (senConfirm = 0 or recConfirm = null)
)
用mongoTemplate的写法
/** * 查询用户所有有效的未取消的见面行程,并按见面时间升序ASC * * @param userId * @return */
@Override
public PageResult<Meeting> getMeetingSchedule(Integer page, Integer limit, Long userId) {
LocalDateTime min = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
Long sevenDaysAgo = AdTimeUtils.getMills(min.plusDays(-7));
// 创建条件
Criteria criteria = new Criteria();
// 启用or运算符,之中只要一条满足就为true则命中
criteria.orOperator(
// 满足条件1
Criteria.where("sendUserId").is(userId)
// 满足条件1的情况下启用or运算符,其中只要一条满足即为true,配合上面的where即为更上层的true
.orOperator(
Criteria.where("senderConfirm").is(MeetingEnum.UN_MEET.getCode()),
Criteria.where("senderConfirm").is(null)
)
,
Criteria.where("recUserId").is(userId)
.orOperator(
Criteria.where("recipientConfirm").is(MeetingEnum.UN_MEET.getCode()),
Criteria.where("recipientConfirm").is(null)
)
);
// 启用and运算符,其中条件都满足即为选中
criteria.andOperator(
Criteria.where("isConsent").is(MeetingEnum.IS_CONSENT.getCode()),
Criteria.where("isMeet").is(MeetingEnum.UN_MEET.getCode()),
Criteria.where("meetTime").gte(sevenDaysAgo)
);
Query query = new Query(criteria);
query.skip((page - 1) * limit).limit(limit)
.with(Sort.by(Sort.Order.asc("meetTime")));
List<Meeting> meetings = mongoTemplate.find(query, Meeting.class);
if (CollUtil.isEmpty(meetings)) {
return new PageResult<>(
false,
CodeEnum.NO_SCHEDULE.getCode());
}
//到这就是查到了需要的行程数据,封装返回
return new PageResult<>(true, page, limit,
CodeEnum.SELECT_SUCCESS.getCode(), meetings);
}
orOperator以及andOperator
都相当于开启了一个and (判断) 当里面的||或者&&满足时这个为true,则命中,嵌套的情况下亦是如此,整体如果判断为true,但是上层有其他的嵌套,则加入他们的判断;
边栏推荐
- 居家办公更要高效-自动化办公完美提升摸鱼时间 | 社区征文
- Promotion of Project Manager
- 杰理之红外滤波【篇】
- 2022 recurrent training question bank and answers for hoisting signal Rigger (special type of construction work)
- Kotlin initialization block
- Kotlin language features
- Eight major trends in the industrial Internet of things (iiot)
- AutoRF:从单视角观察中学习3D物体辐射场(CVPR 2022)
- 杰理之无缝循环播放【篇】
- Why does the kubernetes environment require that bridge NF call iptables be enabled?
猜你喜欢
![[R language data science] (XIV): random variables and basic statistics](/img/87/3606041a588ecc615eb8013cdf9fb1.png)
[R language data science] (XIV): random variables and basic statistics

10 个 Reduce 常用“奇技淫巧”

Developer survey: rust/postgresql is the most popular, and PHP salary is low

万用表测量电阻图解及使用注意事项

Dragon lizard developer said: first time you got an electric shock, so you are such a dragon lizard community| Issue 8

In the era of knowledge economy, it will teach you to do well in knowledge management

常识知识点

Home office should be more efficient - automated office perfectly improves fishing time | community essay solicitation

【5G NR】5G NR系统架构

群晖向阿里云OSS同步
随机推荐
Coinbase will launch the first encryption derivative for individual investors
Kotlin coroutine context and scheduler
Use of kotlin arrays, collections, and maps
Gatling performance test
【5G NR】5G NR系统架构
杰理之增加一个输入捕捉通道【篇】
System status identifier 'hum' for SAP QM inspection lot
图扑软件数字孪生海上风电 | 向海图强,奋楫争先
PM should also learn to reflect every day
如何在物联网低代码平台中进行任务管理?
杰理之TIMER0 用默认的 PA13 来检测脉宽【篇】
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
一个团队可以既做项目又做产品吗?
Eight major trends in the industrial Internet of things (iiot)
kotlin 数组、集合和 Map 的使用
Daily question 8-515 Find the maximum value in each tree row
[one picture series] one picture to understand Tencent Qianfan ipaas
kotlin 组合挂起函数
2022起重信号司索工(建筑特殊工种)复训题库及答案
Prometheus PushGateway 碎碎念