1.Mongodb查询多个对象
import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Query.query;
//条件查询
Query query= query(where("area").is("china").and("age").gte(17));
List<Person> ret= mongoTemplate.find(query,Person.class);
List<Person> ret= mongoTemplate.find(query,Person .class,"collectionName")
2.Mongodb查询单个对象
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import static org.springframework.data.mongodb.core.query.Criteria.where;
//条件
Criteria criteria= where("idCard").is("612650199201121156");
//执行查询
Query query = new Query(criteria);
Person p= mongoTemplate.findOne(query,Person.class);
3.Mongodb分页查询
int offset=0;
int limit=20;
Criteria criteria= where("area").is("china").and("age").gte(17);
Query query = new Query(criteria);
query.with(new Sort(new Sort.Order(Sort.Direction.ASC, "area"),new Sort.Order(Sort.Direction.ASC, "age")));
query.skip(offset).limit(limit);
List<Person> ret= mongoTemplate.find(query,Person.class)
4.Mongodb统计记录数
//条件查询
Criteria cri = Criteria.where("area").is("china").and("age").gte(17);
//执行查询
Query query = Query.query(cri);
//
long count = mongoTemplate.count(query, "collectionName");
long count = mongoTemplate.count(query, Person.class);
5.Mongodb数据聚合分组
//条件
Criteria criteriaDefinition = Criteria.where("user_id").is(userId);
//分组的条件(数组可以是多个) 以及分组后显示的(可以count(),sum(),avg()等) 字段
Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(criteriaDefinition), Aggregation.group("user_id").sum("total_price").as("amount"));
//执行查询
AggregationResults<DBObject> aggregate = mongoTemplate.aggregate(aggregation, "provider_detail_history", DBObject.class);
//得到结果
List<DBObject> mappedResults = aggregate.getMappedResults();
if(Common.isNull_Size(mappedResults))
return RespApi.success(0);
int amount = Integer.parseInt(mappedResults.get(0).get("amount").toString());
6.Mongodb分组得到数据
import org.springframework.data.mongodb.core.aggregation;
//分组
Aggregation agg = Aggregation.newAggregation(
//进行条件的筛选
Aggregation.match(Criteria.where("sellerId").is(sellerId).and("status").is(Status.MercadolibrAnswers.STATUS_1)),
//针对某一个字段分组
Aggregation.group("sku").min("create_time").as("createTime"),
//排序
Aggregation.sort(new Sort(Sort.Direction.ASC,"createTime"))
);
AggregationResults<BasicDBObject> outputType = mongoTemplate.aggregate(agg,"mercadolibre_questions", BasicDBObject.class);
7.Mongodb查询时间段,Mongodb模糊查询,Mongodb动态条件查询,并且分页
/*---------------------------------- service实现-------------------------------------------/
public Page<MercadolibreOrderInfo> getNoPrintOrders(String userId, String keyword, String terrace, Date startDate, Pageable pageable,String sellerId){
startDate = DateUtil.gefirstTimeInday(startDate);
Date endDate = DateUtil.getlastTimeInday(new Date());
BasicDBObject basicDBObject = new BasicDBObject()
.append("user_id",userId)
.append("status",new BasicDBObject("$in",publicStatus()))
.append("order_created_date", new BasicDBObject(QueryOperators.GTE,startDate).append(QueryOperators.LTE,endDate));
if(!Common.isEmpty(terrace))
basicDBObject.put("terrace",terrace);
Pattern nickname = Pattern.compile("^.*"+keyword+".*$", Pattern.CASE_INSENSITIVE);
Pattern order_id = Pattern.compile("^.*"+keyword+".*$", Pattern.CASE_INSENSITIVE);
if(!Common.isEmpty(keyword))
basicDBObject.put("$or",Arrays.asList(
new BasicDBObject().append("buyer.nickname",nickname),
new BasicDBObject().append("order_id",order_id)
)
);
log.info(basicDBObject.toJson());
return mercadolibreOrderRepository.findAllByQuert(basicDBObject,pageable);
}
/*----------------------------------一以下是dao-----------------------------------------*/
@Query(value = "?0")
Page<MercadolibreOrderInfo> findAllByQuert(BasicDBObject dbObject,Pageable pageable);
8 spring boot mongodb 之 @Query 时间段查询
使用 @JsonFormat(pattern="yyyy-mm-dd hh:mm",timezone="GMT+8") 这样的注解,格式化了时间的形式,以上的方式并不会在数据库去格式化时间,只是显示的时候会格式化时间。