1.排序+模糊查詢
Aggregation aggregation1 = Aggregation.newAggregation(Aggregation.match(Criteria.where("shopName").regex("^.*" + "1號" + ".*$")),Aggregation.sort(new Sort(Sort.Direction.DESC,"eventTime")));
List<Map> mappedResults = mongoTemplate.aggregate(aggregation1, "ADD_TO_CART", Map.class).getMappedResults();
mappedResults.stream().forEach(System.out::println);
2.分組+統計
Criteria criteria = Criteria.where("eventTime").regex(date);
// dau uv
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria),
Aggregation.group("channel", "deviceId"),
Aggregation.group("channel").count().as("dau"));
List<Map> map = mongoTemplate.aggregate(agg, "APP_BOOTSTRAP", Map.class).getMappedResults();
map.Stream.forEach(System.out::println)
3.對時間格式做處理(yyyy-MM-dd HH:mm:ss -> yyyy-MM-dd)進行分組排序
//對原油eventTime字段進行處理生成新的date字段
ProjectionOperation as = Aggregation.project("shopId", "date","deviceId").andExpression("eventTime").substring(0, 10).as("date");
GroupOperation group = Aggregation.group("shopId","date","deviceId");
AggregationOperation aggOper = Aggregation.group("shopId","date").count().as("uv");