0

this is my code:

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

The players in the database are the following:

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

The output whould be: (17.0, 18.0, 19.0) but instead I get: "Averages are: 23.0 22.0 24.0"which is more like a Max to me (or maybe just the result from Player 1)... I don't know.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
nuvio
  • 2,555
  • 4
  • 32
  • 58

1 Answers1

1

Typo error:

You should say p2.setClusterId(1) after instantiating player p2

Ankit Bansal
  • 4,962
  • 1
  • 23
  • 40