Java MongoDB聚合不能使用$limit两次
我有以下Java MongoDB聚合不能使用$limit两次,java,mongodb,aggregation-framework,jongo,Java,Mongodb,Aggregation Framework,Jongo,我有以下MongoDBcollection“Games”: 我想将以下MySQL查询映射到MongoDB String query = "SELECT idplayer, COUNT(idplayer) AS countplayer " + "FROM (SELECT b.idgame, b.idplayer " + "FROM associations a, associations b "
MongoDB
collection“Games
”:
我想将以下MySQL查询映射到MongoDB
String query = "SELECT idplayer, COUNT(idplayer) AS countplayer "
+ "FROM (SELECT b.idgame, b.idplayer "
+ "FROM associations a, associations b "
+ "WHERE a.idplayer=? "
+ "AND b.idgame=a.idgame "
+ "AND b.idplayer <> ? "
+ "ORDER BY b.idgame DESC LIMIT 1000) as c"
+ " GROUP BY idplayer "
+ "ORDER BY countplayer DESC LIMIT 5;";
请帮我解决这个问题。谢谢 第一次
组
操作后,字段游戏
不在结果中,因此基于字段游戏
的第二次组
操作不起作用
为了获得更高效的查询,您应该对聚合操作重新排序,以便尽早减少数据。在解开玩家名单之前,我移动了游戏的匹配,因此不需要第二组
在mongo shell中,聚合操作如下所示:
// playerId to search for coplayers
var playerId = "DoomY9999"
db.game.aggregate([
// First $match can take advantage of suitable index if available
// Find all games that playerid X has played
{ $match : { "playersList.playerid" : playerId } },
// Sort by most recent games (gameid descending)
{ $sort : { "_id.gameid" : -1 } },
// Limit number of games to examine
{ $limit : 1000 },
// Create a stream of documents from the playersList array
{ $unwind : "$playersList" },
// Match players except for playerid X
{ $match : { "playersList.playerid" : {$ne : playerId }} },
// Count number of games each player has played
{ $group : {
_id : "$playersList.playerid",
count : { $sum : 1 }
}},
// Sort by most frequent players (count descending)
{ $sort : { "count" : -1 } },
// Limit results to 5 players
{ $limit : 5 },
// Rename the result fields
{ $project : {
_id : 0,
coplayer : "$_id",
count : 1
}}
])
//build the query
DBObject match1 = new BasicDBObject("$match", new BasicDBObject("playersList.playerid",_playerid));
DBObject unwind = new BasicDBObject("$unwind", "$playersList");
DBObject match2 = new BasicDBObject("$match", new BasicDBObject("playersList.playerid",new BasicDBObject("$ne",_playerid)));
DBObject groupFields = new BasicDBObject("_id","$playersList.playerid");
groupFields.put("times", new BasicDBObject("$sum",1));
DBObject group = new BasicDBObject("$group", groupFields);
DBObject sort = new BasicDBObject("$sort", new BasicDBObject("times",-1) );
DBObject limit = new BasicDBObject("$limit", 5 );
DBObject group2 = new BasicDBObject("$group", "gameid");
DBObject sort2 = new BasicDBObject("$sort", new BasicDBObject("gameid",-1) );
DBObject limit2 = new BasicDBObject("$limit", 1000 );
DB db = mongoDb;
DBCollection coll = db.getCollection("games");
//aggregation query
//THIS WORKS
AggregationOutput output = coll.aggregate( match1, unwind, match2, group, sort, limit);
//THIS DOESN'T WORK!
AggregationOutput output = coll.aggregate( match1, unwind, match2, group, sort, limit, group2, sort2, limit2);
// playerId to search for coplayers
var playerId = "DoomY9999"
db.game.aggregate([
// First $match can take advantage of suitable index if available
// Find all games that playerid X has played
{ $match : { "playersList.playerid" : playerId } },
// Sort by most recent games (gameid descending)
{ $sort : { "_id.gameid" : -1 } },
// Limit number of games to examine
{ $limit : 1000 },
// Create a stream of documents from the playersList array
{ $unwind : "$playersList" },
// Match players except for playerid X
{ $match : { "playersList.playerid" : {$ne : playerId }} },
// Count number of games each player has played
{ $group : {
_id : "$playersList.playerid",
count : { $sum : 1 }
}},
// Sort by most frequent players (count descending)
{ $sort : { "count" : -1 } },
// Limit results to 5 players
{ $limit : 5 },
// Rename the result fields
{ $project : {
_id : 0,
coplayer : "$_id",
count : 1
}}
])