当前位置:网站首页>Record various sets of and or of mongotemplate once

Record various sets of and or of mongotemplate once

2022-06-24 13:59:00 Humanoid bug maker 9527

The requirement is to query the user's schedule information
Query the user's itinerary from the past seven days to the future ,
In other words, the data must meet the following conditions
1. Meeting time >= current time -7 God
2. user id= sender id, And the sender confirms the situation =0 perhaps =null
Or the user id= The receiver , And the sender confirms the situation =0 perhaps =null
The above two indicate that the user has not confirmed or denied the record , So show
3. The itinerary must be agreed
4. The itinerary must be unseen
5. Designated user


mysql General writing

select
*
from table
where 
isConsent = 1
and
isMeet = 0
and
userId = xxxxxxxxx
and meetime >  Current time minus 7 God 
and
(
	userId = recId and (recConfirm = 0 or recConfirm = null)
	or
	userId = senId and (senConfirm = 0 or recConfirm = null)
)

use mongoTemplate Writing

/** *  Query all valid and UN cancelled meeting itineraries of the user , And in ascending order of meeting time 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));
    //  Create conditions 
    Criteria criteria = new Criteria();
    //  Enable or Operator , As long as one of them is satisfied, it is true Then hit 
    criteria.orOperator(
            //  Meet the conditions 1
            Criteria.where("sendUserId").is(userId)
                    //  Meet the conditions 1 Enable... When or Operator , As long as one of them is satisfied, it is true, Match the above where That is, the higher level 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)
                    )
    );
    //  Enable and Operator , If all the conditions are met, it is selected 
    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());
    }
    // This means that the required travel data has been found , Package return 
    return new PageResult<>(true, page, limit,
            CodeEnum.SELECT_SUCCESS.getCode(), meetings);
}

orOperator as well as andOperator
Are equivalent to opening a and ( Judge ) When inside || perhaps && When satisfied, this is true, Then hit , The same is true for nested cases , If the whole is judged as true, But the upper layer has other nesting , Then add their judgment ;

原网站

版权声明
本文为[Humanoid bug maker 9527]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241120427040.html