zoukankan      html  css  js  c++  java
  • MongoDBTemplate多条件查询的问题

    问题:

    在使用Spring Data MongoDB 进行条件查询数据时,发现条件判断不起作用,结果会返回所有的数据。

    Criteria criteria = new Criteria();
                criteria.where("shopId")
                        .is(request.getShopId())
                        .and("tradeDate").gte(request.getBeginTradeDate())
                        .lte(request.getEndTradeDate());
                List<ReportInfo> reportInfoList = reportMongoTemplate.
                        find(new Query(criteria), ReportInfo.class,"rept_wallet_refundAndPay");
    

    原因:

    经过查看文档和源代码才发现,Criteria的where方法是一个静态工厂方法,它会返回一个实例化的criteria对象,所以就不需要自己new 一个criteria对象了。否则 find(new Query(criteria)里的criteria没有任何判断条件,因此会返回所有的数据。

    MongoTemplate 源码

        public static Criteria where(String key) {
            return new Criteria(key);
        }
    

    修改

    Criteria criteria = Criteria.where("shopId")
                        .is(request.getShopId())
                        .and("tradeDate").gte(request.getBeginTradeDate())
                        .lte(request.getEndTradeDate());
                List<ReportInfo> reportInfoList = reportMongoTemplate.
                        find(new Query(criteria), ReportInfo.class,"rept_wallet_refundAndPay");
    

    总结:

    还是要看官方的文档和源代码

  • 相关阅读:
    线段树(已修改+补题
    畅通工程
    线段树
    charles
    flash
    mysql node pool
    node简单操作mysql的类
    小于任意数字的随机数
    文件系统的移植
    驱动
  • 原文地址:https://www.cnblogs.com/lanqi/p/7978048.html
Copyright © 2011-2022 走看看