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の結果かもしれません)...わかりません。

4

1 に答える 1

1

入力ミス:

p2.setClusterId(1)プレーヤーp2をインスタンス化した後に言う必要があります

于 2013-03-25T15:56:01.377 に答える