MongoDB聚合不能使用$ limit两次

时间:2013-03-29 13:45:11

标签: java mongodb aggregation-framework jongo

我有以下MongoDB集合“Games”:

{ 
"_id" : ObjectId("515461d3c6c18efd4a811fd3"), 
"gameid" : NumberLong("86982207656"), 
"tableName" : "Hydra Zoom 40-100 bb", 
"nplayers" : 6, 
"playersList" : [   
                                    {           "exist" : true,         
                                                        "suspended" : false,
                                                        "grade" : 0,
                                                        "clusterId" : -1,
                                                        "playerid" : "DoomY9999",
                                                        "playsWithFriends" : 0,
                                                        "squeezePlay" : 0,
                                                        "weakShowdown" : 0,
                                                        "numberOfPlays" : 1
                                     },
                                  {
                                                        "exist": true,
                                                        "suspended" : false,

我想将以下 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;";

查询说明:此SQL查询计算最常播放玩家'X'游戏的最常见玩家。结果将是球员的名字和一起比赛的次数

LIMIT的简短说明:第一个“LIMIT 1000”实际上将限制我们想要检查的游戏,因为数据库可能非常大,我们只按DESC顺序分析最近1000个游戏(最近一次)有更高的“gameid”。

第二个限制5:适用于“前5名”朋友。我们将总结他们的数字。

到目前为止,我已完成:几乎所有使用聚合框架的内容都是"ORDER BY b.idgame DESC LIMIT 1000) as c"的例外。这对我很重要,因为它经历的游戏数量可能非常高。

以下是 MongoDB (Java驱动程序)中的查询:

 //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); 

请帮我修复此查询。谢谢!

1 个答案:

答案 0 :(得分:2)

第一个game操作后,字段group不在结果中,因此基于字段group的第二个game操作将无效

为了更有效的查询,您应该重新排序聚合操作以尽可能早地减少数据。在展开playersList之前我移动了游戏的匹配,没有必要拥有第二组。

聚合操作在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
    }}
])