当前位置:网站首页>记录一次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
,但是上层有其他的嵌套,则加入他们的判断;
边栏推荐
- Kotlin initialization block
- 杰理之检测 MIC 能量自动录音自动播放参考【篇】
- Kotlin coroutine context and scheduler
- 知识经济时代,教会你做好知识管理
- Can inspection results be entered after the completion of inspection lot UD with long-term inspection characteristics in SAP QM?
- Gatling 性能测试
- The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
- How does webrtc obtain video stream data on the C ++ side?
- The second phase of freshman engineering education seminar is to enroll in the China 100 school peer program
- 杰理之.睡眠以后定时唤醒系统继续跑不复位【篇】
猜你喜欢
Tupu software is the digital twin of offshore wind power, striving to be the first
Activity生命周期
开发者调查:Rust/PostgreSQL 最受喜爱,PHP 薪水偏低
【R语言数据科学】(十四):随机变量和基本统计量
【5G NR】5G NR系统架构
快手实时数仓保障体系研发实践
Gatling 性能测试
How to avoid serious network security accidents?
Vulnerability management mistakes that CIOs still make
Eight major trends in the industrial Internet of things (iiot)
随机推荐
PM should also learn to reflect every day
MySQL interview questions
源码解析 Handler 面试宝典
[R language data science] (XIV): random variables and basic statistics
Cloud native essay solicitation progress case practice
2022 Quality Officer - Equipment direction - post skills (Quality Officer) recurrent training question bank and online simulation examination
10 reduce common "tricks"
如何避免严重网络安全事故的发生?
[one picture series] one picture to understand Tencent Qianfan apaas
Explain the difference between iaas/paas/saas by cooking rice
谷歌WayMo提出R4D: 采用参考目标做远程距离估计
CPU process priority
Coinbase will launch the first encryption derivative for individual investors
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
Source code analysis handler interview classic
HarmonyOS-3
What is the difference between sap QM and UD for inspection lots with hum?
**Puzzling little problem in unity - light and sky box
How to avoid serious network security accidents?
Ti Xing Shu'an joined the dragon lizard community to jointly create a network security ecosystem