mongo 聚合操作 Java中代碼

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


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