当前位置:   article > 正文

Java操作mongodb(含分页,精确查询,模糊查询,时间区间,排序)进行查询_java mongodb模糊查询

java mongodb模糊查询

mongodb是常用的非关系型数据库,他经常用来存储文本数据,也就是JSON格式的数据。
不废话,直接上代码。注释写的很详细。(有问题留言秒回)

    public Page<Product> listProducts(ProductCond cond) {
        //如前端没传,就默认第一页,前10条
        if(cond.getSize()==null ||  cond.getCurrent() ==null) {
            cond.setSize(1);
            cond.setCurrent(10);
        }
        Query query = new Query();
        query.addCriteria(Criteria.where("data_status").is(1L));
        query.addCriteria(Criteria.where("tenant_id").is(LoginUserContext.getContext().getTenantId()));
        //手动拼接查询参数
        if(StringUtils.isNotBlank(cond.getProductSoleCode())){
            query.addCriteria(Criteria.where("product_sole_code").is(cond.getProductSoleCode()));
        }
        //.regex为模糊查询
        if(StringUtils.isNotBlank(cond.getProductTemplateName())){
            query.addCriteria(Criteria.where("product_template_name").regex(".*?" + cond.getProductTemplateName() + ".*"));
        }
        if(StringUtils.isNotBlank(cond.getProductTypeName())){
            query.addCriteria(Criteria.where("product_type_name").is(cond.getProductTypeName()));
        }
        //设置时间区间
        if(StringUtils.isNotBlank(cond.getStartCreateTime()) && StringUtils.isNotBlank(cond.getEndCreateTime())){
            Date startTime = strToDateLong(cond.getStartCreateTime());
            Date endTime = strToDateLong(cond.getEndCreateTime());
            query.addCriteria(Criteria.where("create_time").gte(startTime).lte(endTime));
        }
        //查全部的条数
        long total = mongoTemplate.count(query,Product.class);
        //手动拼接分页参数+根据创建时间倒叙
        query.skip((cond.getCurrent() - 1L) *cond.getSize()).limit(cond.getSize()).with(Sort.by(Sort.Direction.DESC, "create_time"));

        log.info("查询mongodb产品实例的条件为:{}", query);
        List<Product> userList = mongoTemplate.find(query,Product.class);
        //手动拼接分页结果
        long pages = (long)Math.ceil(total/cond.getSize()) ;
        Page<Product> pageList = new Page<>();
        pageList.setRecords(userList);
        pageList.setTotal(total);
        pageList.setPages(pages);
        pageList.setCurrent(cond.getCurrent());
        pageList.setSize(cond.getSize());
        return pageList;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

mongoTemplate可以进行区间查询,MongoRepository反正我是没找到他能区间查询的方法。所以查询推荐使用mongoTemplate进行区间查询。
查询结果:在这里插入图片描述
参考以下内容:
https://blog.csdn.net/weixin_43059031/article/details/100079941
https://blog.csdn.net/a1120467800/article/details/109954145
https://blog.csdn.net/weixin_44216706/article/details/106496298
https://blog.csdn.net/cheryjava/article/details/120655671
https://blog.csdn.net/qq_38359685/article/details/113865582
https://blog.csdn.net/xjx891111/article/details/120371529
https://blog.csdn.net/qq_43718308/article/details/108128461
https://blog.csdn.net/edc0228/article/details/92645396

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/56068
推荐阅读
相关标签
  

闽ICP备14008679号