0

这是我数据库中约 1000 万行中的一行:

{
    "_id" : ObjectId("58f569159f809c49ffc5cdbb"),
    "date" : ISODate("2017-03-21T09:10:07.686Z"),
    "p1" : {
        "_id" : 1765906,
        "deck" : [ 
            33, 
            25, 
            59, 
            38, 
            62, 
            3, 
            33, 
            57
        ],
        "crowns" : 3
    },
    "p2" : {
        "_id" : 2520156,
        "deck" : [ 
            25, 
            69, 
            86, 
            8, 
            44, 
            61, 
            69, 
            50
        ],
        "crowns" : 2
    }
}

这是一个游戏战斗的日志。牌组是一个阵列,由 8 张不同的牌组成。我正在尝试找到获胜次数最多的卡。可以通过比较皇冠来确定获胜者(我设法选择了玩家 1 获胜的所有文件)。

我想要达到的目标:

  1. 不幸的是,我无法执行组查询来返回我正在寻找的内容(获胜次数最多的卡片)。

  2. 我也在尝试找到最成功的套牌(获胜的数量就足够了——应该忽略该数组中指定的卡片顺序)。

我已经尝试过,但一段时间后返回了一个空错误:

db.battle_logs.aggregate([
    {
        $addFields: {
            "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] }
        }
    },
    { $match: { "player1won": 1 }},
    { 
        $group: 
        {
            _id: "$p1.deck",
            count: { $sum: 1 }
        }
    }
], {
  allowDiskUse:true,
  cursor:{}
 })

我希望将所有套牌组合按其获胜次数分组(仅考虑 player1 获胜)

4

1 回答 1

1

当您在组查询中使用数组p1.deck_id,您需要对数组进行排序,否则卡片的顺序将创建一个新组。

这可能适用于您的情况:

1)

db.battle_logs.aggregate([
    {
        $addFields: {
            "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] }
        }
    },
    //Fiter out ties
    { 
        $match: { 
            "$or" : [
                {"player1won": 1},  //Player 1 won
                {"player1won" : -1} //Player 2 won
            ]
        }
    },
    // Get the winning deck
    {
        $project: {
            "deck": { $cond : [ { $gte : [ "$p1.crowns", "$p2.crowns" ]}, "$p1.deck", "$p2.deck" ]} 
        }
    },
    //Unwind the deck to get every card
    { 
        $unwind : "$deck"
    },
    //Get count of each card
    { 
        $group : { 
            "_id" : "$deck" , 
            "count" : {"$sum" : 1}
        }
    }, 
    // Sort on count
    {
        $sort: {"count" : -1}

    },
    //Get the card with highest count
    { 
        $limit: 1
    }
], {
  allowDiskUse:true,
  cursor:{}
 })

2)

db.battle_logs.aggregate([
    {
        $addFields: {
            "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] }
        }
    },
    //Fiter out ties
    { 
        $match: { 
            "$or" : [
                {"player1won": 1},  //Player 1 won
                {"player1won" : -1} //Player 2 won
            ]
        }
    },
    // Get the winning deck
    {
        $project: {
            "deck": { $cond : [ { $gte : [ "$p1.crowns", "$p2.crowns" ]}, "$p1.deck", "$p2.deck" ]} 
        }
    },
    //Unwind the deck to get every card
    { 
        $unwind : "$deck"
    },
    //Sort the cards
    { 
        $sort : {"deck": 1}
    },
    //Group sorted cards back in deck
    { 
        $group : { 
            "_id" : "$_id" , 
            "deck" : {"$push" : "$deck"}
        }
    }, 
    { 
        $group: 
        {
            _id: "$deck",
            count: { $sum: 1 }
        }
    }
], {
  allowDiskUse:true,
  cursor:{}
 })
于 2017-04-18T23:10:29.410 回答