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")));