MongoDB Java 查询

分页查询等

 @RequestMapping("getWsMonitorInfo")
    public String getWsMonitorInfo(MonitorInfo monitorInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        String type = request.getParameter("type");
        Criteria criteria = new Criteria();
        if(StringUtils.isBlank(monitorInfo.getReqSyscode())){
            monitorInfo.setReqSyscode(null);
        }
        if(StringUtils.isBlank(monitorInfo.getServiceName())){
            monitorInfo.setServiceName(null);
        }
        Page<MonitorInfo> monitorInfoPage = null;
        Query query = new Query();

        if (type != null) {

            switch (type) {
                case "startPoint":
//                    criteria=    Criteria.where("reqSyscode").is(null).and("serviceName").is(null);
                    monitorInfo.setReqSyscode(null);
                    monitorInfo.setServiceName(null);
                    break;
                case "callSystem":
                    criteria=   Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName());
                    break;
                case "totalCall":
                    criteria=  Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName());
                    break;
                case "successCall":
                    criteria=    Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName()).and("ifSuccess").is("1");
                    monitorInfo.setIfSuccess("1");
                    break;
                case "failCall":
                    criteria=    Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName()).and("ifSuccess").is("0");
                    monitorInfo.setIfSuccess("0");
                    break;
                case "proxyService":
                    criteria=    Criteria.where("serviceName").is(monitorInfo.getServiceName());
                    monitorInfo.setReqSyscode(null);
                    break;
            }
            monitorInfoPage = new Page<>(0, 10);
        } else {
            criteria= null;
            if(StringUtils.isNotBlank(monitorInfo.getReqSyscode())){
                query.addCriteria( Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()));
            }
            if(StringUtils.isNotBlank(monitorInfo.getServiceName())){
                query.addCriteria( Criteria.where("serviceName").is(monitorInfo.getServiceName()));
            }
            if(StringUtils.isNotBlank(monitorInfo.getIfSuccess())){
                query.addCriteria( Criteria.where("ifSuccess").is(monitorInfo.getIfSuccess()));            }
            }
            monitorInfoPage = new Page<>(request, response);

        Sort sort = new Sort(Sort.Direction.DESC,"createDate");
        if(criteria!=null){
            query.addCriteria(criteria);
        }
        Long total = mongoOperations.count(query,MonitorInfo.class);
        query.skip(((monitorInfoPage.getPageNo()-1)>0?monitorInfoPage.getPageNo()-1:0)*monitorInfoPage.getPageSize()).limit(monitorInfoPage.getPageSize()).with(sort);
        List<MonitorInfo> monitorInfos = mongoOperations.find( query,MonitorInfo.class);
        monitorInfoPage.setCount(total);
        monitorInfoPage.setList(monitorInfos);
//        Page<MonitorInfo> page = monitorInfoService.findPage(monitorInfoPage, monitorInfo);
        model.addAttribute("page", monitorInfoPage);
        return "bi/monitor/monitorInfoList";

    }

 

注意点: mongon中 字段存在和字段为null不等价。空串和null也不等价。

字段不存在 Criteria.where("ifSuccess").not();且不能与其他条件用and或者or拼接

字段为空 Criteria.where("ifSuccess").is(null);

字段为空串 Criteria.where("ifSuccess").is("");

建索引

db.monitorInfo.createIndex(
    {
        reqSystemCode: 1,
        serviceName: 1,
        createDate: 1
    },
    {
        name: "reqSystemCode_1_serviceName_1_createDate_1"
    }
)

 

更新db.monitorInfo.update({}, 
    {$set:{}}, 
    { multi: false, upsert: false}
)

multi:是否全部更新,upsert是否插入更新(找不到一样的就插入);

查询不存在某个字段的数据

db.getCollection(‘test’).find({res_name:{$exists:false}})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值