一言难尽
发布于

记录一下java操作mongodb多条件动态分页查询代码

Info info = new Info();
            PageRequest pageRequest = PageRequest.of(0,5);
            Example<Info> infoExample = Example.of(info);
            infoExample.getMatcher().withIgnorePaths("lastProcessedTime");
            Query query = new Query(Criteria.byExample(infoExample));
            Criteria criteria = new Criteria();
            Criteria nb = new Criteria();
            nb.and("type").is(4).and("docChapters").elemMatch(Criteria.where("lastProcessedTime").exists(false));
            Criteria ts = new Criteria();
            ts.and("type").is(3).and("lastProcessedTime").exists(false).and("content").exists(true);
            criteria.orOperator(nb,ts);
            query.addCriteria(criteria);
            query.with(Sort.by(
                    Sort.Order.desc("createTime")
            ));
            query.with(pageRequest);
            long count = mongoTemplate.count(query, Info.class);
            Page<Info> page = new PageImpl<>(mongoTemplate.find(query, Info.class), pageRequest, count);
            List<Info> infos = page.getContent();
评论