我需要指导将以下MongoDB查询转换为使用Java DB驱动程序检索数据的Java程序。任何指导都会非常感激。
db.playerscorecollection.aggregate([
{
$unwind: "$scorearray"
},
{
$group:
{
_id: {player:'$player,venue:'$venue'}, maxScore: { $max: "$scorearray.score" }
}
}
]);
答案 0 :(得分:1)
http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/的文档和教程非常广泛。在阅读本答案的其余部分之前,请先阅读第一个。一般来说,这样的事情应该有效:
// 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 );