初学MongoDB

1.排序+模糊查询

Aggregation aggregation1 = Aggregation.newAggregation(Aggregation.match(Criteria.where("shopName").regex("^.*" + "1号" + ".*$")),Aggregation.sort(new Sort(Sort.Direction.DESC,"eventTime")));
        List 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 = mongoTemplate.aggregate(agg, "APP_BOOTSTRAP", Map.class).getMappedResults();
        map.Stream.forEach(System.out::println)

 

你可能感兴趣的:(mongoDB)