I need guidance in translating the following MongoDB query in to a Java program that uses a Java DB driver to retrieve data. Any guidance would be really appreciated.
db.playerscorecollection.aggregate([
{
$unwind: "$scorearray"
},
{
$group:
{
_id: {player:'$player,venue:'$venue'}, maxScore: { $max: "$scorearray.score" }
}
}
]);
The documentation and tutorial at http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/ is quite extensive. Please read that first before reading the remainder of this answer. In general, something like this should work:
// create our pipeline operations, first the $unwind
DBObject unwind = new BasicDBObject( "$unwind", "$scorearray" );
DBObject groupIdFields = new BasicDBObject( "player", "$player" );
groupIdFields.put( "venue", "$venue" );
DBObject groupFields = new BasicDBObject( "_id", groupIdFields );
groupFields.put( "maxScore", new BasicDBObject( "$max", "$scorearray.score" ) );
DBObject group = new BasicDBObject( "$group", groupFields );
// run aggregation
AggregationOutput output = MDB.getCollection("playerscorecollection").aggregate( unwind, group );