springboot mongoTemplate多条件查询,区间查询,表名查询,排序

mongodbTemplate实现时间区间查询、排序、返回指定字段、指定表名查询

@Autowired
private MongoTemplate mongoTemplate;

@Override
public ResultData rawDataQueryMethod(QueryDataCurrencyParamDTO queryDataCurrencyParamDTO) {

    List<DBObject> andCondi = new ArrayList<DBObject>();
    DBObject userIdObject = new BasicDBObject();
    if(null!=queryDataCurrencyParamDTO.getUser_id() && !queryDataCurrencyParamDTO.getUser_id().equals("")){
        userIdObject.put("user_id",queryDataCurrencyParamDTO.getUser_id());
        andCondi.add(userIdObject);
    }
    DBObject gadgetIdObject = new BasicDBObject();
    if(null!=queryDataCurrencyParamDTO.getGadget_id() && !queryDataCurrencyParamDTO.getGadget_id().equals("")){
        gadgetIdObject.put("gadget_id",queryDataCurrencyParamDTO.getGadget_id());
        andCondi.add(gadgetIdObject);
    }
    DBObject gadgetTypeIdObject = new BasicDBObject();
    if(null!=queryDataCurrencyParamDTO.getGadget_type_id() && !queryDataCurrencyParamDTO.getGadget_type_id().equals("")){
        gadgetTypeIdObject.put("gadget_type_id",queryDataCurrencyParamDTO.getGadget_type_id());
        andCondi.add(gadgetTypeIdObject);
    }

    DBObject timeObject = new BasicDBObject();
    if(null!=queryDataCurrencyParamDTO.getStart_time() && !queryDataCurrencyParamDTO.getStart_time().equals("")){
        timeObject.put("$gte",Long.valueOf(queryDataCurrencyParamDTO.getStart_time()));
    }
    if(null!=queryDataCurrencyParamDTO.getEnd_time() && !queryDataCurrencyParamDTO.getEnd_time().equals("")){
        timeObject.put("$lte",Long.valueOf(queryDataCurrencyParamDTO.getEnd_time()));
    }
    if(timeObject.keySet().size()>0){
        DBObject timeCompare = new BasicDBObject();
        timeCompare.put("time",timeObject);
        andCondi.add(timeCompare);
    }

    BasicDBObject andQuery = new BasicDBObject();
    andQuery.put("$and", andCondi);

    DBObject fieldsObject = new BasicDBObject();
    fieldsObject.put("time", true);
    fieldsObject.put("value", true);
    Query query = new BasicQuery(andQuery,fieldsObject);
    query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "time")));
    System.out.println(query.toString());

    try {
          List<AvgData> rawDataList = mongoTemplate.find(query,AvgData.class,"declad_log_data");
        if(null!=rawDataList && rawDataList.size()>0){
            return ResultData.ok(rawDataList);
        }else {
            return ResultData.ok(rawDataList);
        }
    }catch (Exception e){
        e.printStackTrace();
        return ResultData.error();
    }
}

 

 

Criteria criteria = new Criteria();

if(null!=queryDataCurrencyParamDTO.getUser_id() && !queryDataCurrencyParamDTO.getUser_id().equals("")){
    criteria.and("user_id").is(queryDataCurrencyParamDTO.getUser_id());
}
if(null!=queryDataCurrencyParamDTO.getGadget_id() && !queryDataCurrencyParamDTO.getGadget_id().equals("")){
    criteria.and("gadget_id").is(queryDataCurrencyParamDTO.getGadget_id());
}
if(null!=queryDataCurrencyParamDTO.getGadget_type_id() && !queryDataCurrencyParamDTO.getGadget_type_id().equals("")){
    criteria.and("gadget_type_id").is(queryDataCurrencyParamDTO.getGadget_type_id());
}

boolean timeFlag=false;
Criteria timeCriteria = Criteria.where("time");
if(null!=queryDataCurrencyParamDTO.getStart_time() && !queryDataCurrencyParamDTO.getStart_time().equals("")){
    timeFlag=true;
    timeCriteria.gte(Long.valueOf(queryDataCurrencyParamDTO.getStart_time()));
}
if(null!=queryDataCurrencyParamDTO.getEnd_time() && !queryDataCurrencyParamDTO.getEnd_time().equals("")){
    timeFlag=true;
    timeCriteria.lte(Long.valueOf(queryDataCurrencyParamDTO.getEnd_time()));
}
if(timeFlag){
    criteria.andOperator(timeCriteria);
}

Query query = new Query(criteria);
Field fields  = query.fields();
fields.include("time");
fields.include("value");
query.with(new Sort(new Sort.Order(Sort.Direction.DESC, "time")));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值