使用的是spring-data-mongodb这个包
1、单条件查询:
private MongoTemplate mongoTemplate;
Query query = new Query();
query.addCriteria(Criteria.where("loginId").is(loginId));
Sort sort = new Sort(Sort.Direction.ASC, "createTime");// DESC顺序 ASC逆序
query.with(sort);
List<RemarkRecord> members = mongoTemplate.find(query, RemarkRecord.class, "xf");
2、多条件查询:
query.addCriteria(Criteria.where("loginId").is(loginId));//这类语句多加几个就可以实现多条件查询
3、聚合:
Criteria criteria = Criteria.where("type").exists(false).and("identity").ne(1);
Aggregation egg = Aggregation.newAggregation(Aggregation.match(criteria),Aggregation.group("loginId").count().as("count"));
egg = Aggregation.newAggregation(Aggregation.match(criteria), Aggregation.group("loginId").count().as("count"));
AggregationResults<ChatCountEntity> lastWeekChatNumList = mongoTemplate.aggregate(egg, "xf", ChatCountEntity.class);
以上的代码与上一篇mongodb语句有关联。