我有以下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);
请帮我解决这个查询。谢谢!