0

我需要指导将以下 MongoDB 查询转换为使用 Java DB 驱动程序检索数据的 Java 程序。任何指导将不胜感激。

db.playerscorecollection.aggregate([ 

{ 

 $unwind: "$scorearray"

}, 

{ 
$group: 
{ 

   _id: {player:'$player,venue:'$venue'}, maxScore: { $max: "$scorearray.score" } 

} 

} 

]); 
4

1 回答 1

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 );
于 2013-07-11T07:38:46.060 回答