初學MongoDB(個人總結)

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章