【问题标题】:Group By Count on Array MongoDB数组 MongoDB 按计数分组
【发布时间】:2017-09-13 17:50:24
【问题描述】:

这是我数据库中约 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 获胜)

【问题讨论】:

  • 不是很清楚,能否为您提供的文档添加预期的输出?
  • @felix For 2) 它将是带有获胜次数/次数的套牌(这是一个由整数组成的数组)(我已经在给定的查询中过滤了 player1Won,所以我只需要按牌组分组并计算结果)。我假设我需要对 int 数组 (p1.deck) 进行排序,以便它忽略卡片的顺序

标签: mongodb mongodb-query aggregation-framework nosql-aggregation


【解决方案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:{}
 })

【讨论】:

    猜你喜欢
    • 2019-03-09
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2023-04-06
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多