mongo 聚合操作 Java中代码

来源:互联网 发布:复杂网络 pdf 编辑:程序博客网 时间:2024/06/04 00:20
1、Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("month")
.is(month).and("createTime")
.gte(startDate).lt(endDate)
.and("result").is(1),
Aggregation.group("month").count()
.as("NUmCount"));// 分组条件:月份


List<MyObject> listist = mongoTemplate.aggregate(aggregation, collectionName, MyObject.class).getMappedResults();

mongoTemplate.insert(listist, newCollectionName);


2、Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("id").is(id).and("createTime").gte(startDate).lt(endDate)), 
Aggregation.group("money").count().as("totalOrder").sum("age").as("totalage")
.addToSet("agePercent").as("agePercent").addToSet("ageLimited").as("ageLimited"));

List<MyObject> listist = mongoTemplate.aggregate(aggregation, collectionName, MyObject.class).getMappedResults();
mongoTemplate.insert(listist, newCollectionName);


原创粉丝点击