0
,這是我的代碼:
//Build the query
//match only records in cluster 1
DBObject match = new BasicDBObject("$match", new BasicDBObject("clusterId",_id));
//the projected result must be : clusterId, squeezePlay, weakShowdown, playsWithFriends
DBObject projectFields = new BasicDBObject("clusterId",1);
projectFields.put("squeezePlay", 1);
projectFields.put("weakShowdown", 1);
projectFields.put("playsWithFriends", 1);
DBObject project = new BasicDBObject("$project", projectFields);
//the groupfields are the average of each category grouped by clusterId
DBObject groupFields = new BasicDBObject("_id",new BasicDBObject("clusterId","$clusterId"));
groupFields.put("squeezePlay", new BasicDBObject("$avg","$squeezePlay"));
groupFields.put("weakShowdown", new BasicDBObject("$avg","$weakShowdown"));
groupFields.put("playsWithFriends", new BasicDBObject("$avg","$playsWithFriends"));
DBObject group = new BasicDBObject("$group", groupFields);
AggregationOutput output = coll.aggregate(match, project, group);
在數據庫中的球員如下:
Player p1 = new Player();
p1.SetClusterID(1);
p1.SetId("Player 1");
p1.SetNumberOfPlays(21);
p1.SetPlaysWithFriends(22);
p1.SetSqueezePlay(23);
p1.SetWeakShowdown(24);
Player p2 = new Player();
p1.SetClusterID(1);
p2.SetId("Player 2");
p2.SetNumberOfPlays(11);
p2.SetPlaysWithFriends(12);
p2.SetSqueezePlay(13);
p2.SetWeakShowdown(14);
輸出對子級爲:(17.0,18.0,19.0) 但相反,我得到:"Averages are: 23.0 22.0 24.0"
這對我來說更像是一個Max(或者也許只是玩家1的結果)......我不知道。
哦後說
p2.setClusterId(1)
謝謝我得到了它 – nuvio 2013-03-25 15:58:06