2

I am using the MongoDB aggregate framework to query a document, the results is the following:

{
        "result" : [
                {
                        "_id" : "luke",
                        "times" : 8
                },
                {
                        "_id" : "albert",
                        "times" : 4
                },
                {
                        "_id" : "matt",
                        "times" : 4
                }
        ],
        "ok" : 1
}

As you can see from the result above, the query works in the mongoDB shell, but I have a problem when getting the results with Jongo:

        Aggregationoutput = 
                gamesCollection.aggregate(
                "{ ... }"
                ).as(Aggregation.class);
        output.results().iterator().hasNext();

The main problem seems to be that Jongo doesn't allow me to use AggregationOutput? he wants instead Aggregation ... but can't find any example available on how to use it

EDIT: I am a bit frustrated that I can't make Jongo to work with the aggregate. I had to write the query with DBObjects as specified in the MongoDB Java driver, but the code looks really ugly..

EDIT2: Just to complete the information, this is the original aggregate I was using with Jongo which could not unmarshall to ResultObject

List<ResultObject> output = 
                gamesCollection.aggregate(
                "{ $match: { 'playersList.playerid': 'bob' }},"
                +"{ $unwind: '$playersList' },"
                +"{ $match: { 'playersList.playerid':  { $ne: 'bob' } } },"
                +"{ $group: { _id: '$playersList.playerid', times: { $sum : 1} } },"
                +"{ $sort: { times: -1 } }"
                ).as(ResultObject.class);

class ResultObject{
  String _id;
  int times;
}

}

nuvio
  • 2,555
  • 4
  • 32
  • 58
  • Can you please share the code of the Aggregation class and the complete collection.aggregate(..) that you're querying with Jongo? Thanks. – yves amsellem Mar 24 '13 at 15:36
  • I'll go for the quick and dirty solution, when I got time I'll go back and revise your suggestions. Thanks very much for the help – nuvio Mar 24 '13 at 17:17
  • As you wish. It seems to me that with your complete code, we can solve this in a couple of minutes. – yves amsellem Mar 24 '13 at 17:20

2 Answers2

4

You can use aggregate feature like find/findOne... Results are automatically unmarshalled into Pojo :

List<Email> emails = collection.aggregate("{$project:{sender:1}}")
          .and("{$match:{tags:'read'}}")
          .and("{$limit:10}")
          .as(Email.class);

You can find more examples here : https://github.com/bguerout/jongo/blob/master/src/test/java/org/jongo/AggregateTest.java

Benoît Guérout
  • 1,977
  • 3
  • 21
  • 30
0

I just saw this question now, but I hope it can help others. You can create an inner class like this:

private static class AggregateResult {
        String _id;
        int time;
    }

And call the aggregate function as following:

List<AggregateResult> res =  gamesCollection.aggregate(
                "{ ... }"
                ).as(AggregateResult.class);

Then you can iterate over the results in the res list.

noamdayan
  • 175
  • 3
  • 9