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