在mongo中 aggregation(聚合应用)
1、插入测试数据
db.tycoon.insertMany( [ {item:"paper",tycoon:25,size:{h:14, w:21,cm:"and" },status:"A"}, {item:"notebook",tycoon:50,size:{h:8.5, w:11,cm:"in" },status:"B"}, {item:"pencil",tycoon:100,size:{h:8.5, w:11,cm:"max" },status:"C"}, {item:"schoolbag",tycoon:75,size:{h:1.85, w:30,cm:"avg" },status:"D"}, {item:"ruler",tycoon:45,size:{h:10, w:15.25,cm:"count" },status:"E"}]);
2、java 测试
Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("size.h").gte(9.0)), Aggregation.unwind("size"), Aggregation.group("$size.cm").avg("$size.w").as("平均").min("$size.w").as ("MinValue").max("$size.w").as("MaxValue").sum("$size.w").as("SumItem")); AggregationResults<BasicDBObject> result= template.aggregate(aggregation, "tycoon", BasicDBObject.class); for (Iterator<BasicDBObject> iterator = result.iterator(); iterator.hasNext(); ) { DBObject obj = iterator.next(); System.out.println(obj); } }
标签:status,tycoon,mongo,cm,Aggregation,aggregation,size From: https://www.cnblogs.com/northeastTycoon/p/18081964