【问题标题】:Get all possible combinations from array in MongoDB aggregation ????从 MongoDB 聚合中的数组中获取所有可能的组合????
【发布时间】:2021-02-24 23:20:57
【问题描述】:

如何通过数组中的相同值进行聚合($group)?不是一次全部,而是很少全部,如果有的话。 $group 一个字我都可以,但我还需要所有可能的变化……

集合示例:

{"keywords": ["gta", "distribution", "keys"]}
{"keywords": ["gta", "online", "moto", "races"]}
{"keywords": ["gta", "online", "samp"]}

结果示例:

  1. “gta” - 3 个匹配项
  2. “在线” - 2 个匹配项
  3. “gta online” - 2 场比赛

【问题讨论】:

    标签: mongodb aggregation-framework match aggregation


    【解决方案1】:

    您可以使用$reduce 从数组中提取所有对的组合。我从this post 开始,并添加了当前项目$unwind 初始数组并计算项目数:

    db.test.aggregate([
        {
            $project: {
                pairs: {
                    $reduce: {
                        input: { $range: [0, { $size: "$keywords" }] },
                        initialValue: [],
                        in: {
                            $concatArrays: [
                                "$$value",
                                [[{ $arrayElemAt: ["$keywords", "$$this"] }]],
                                {
                                    $let: {
                                        vars: { i: "$$this" },
                                        in: {
                                            $map: {
                                                input: { $range: [{ $add: [1, "$$i"] }, { $size: "$keywords" }] },
                                                in: [{ $arrayElemAt: ["$keywords", "$$i"] }, { $arrayElemAt: ["$keywords", "$$this"] }]
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }, {
            $unwind: "$pairs"
        }, {
            $group: {
                _id: "$pairs",
                count: { $sum: 1 }
            }
        }
    ])
    

    输出:

    { "_id" : [ "online", "samp" ], "count" : 1 }
    { "_id" : [ "gta", "samp" ], "count" : 1 }
    { "_id" : [ "online", "races" ], "count" : 1 }
    { "_id" : [ "moto", "races" ], "count" : 1 }
    { "_id" : [ "gta", "keys" ], "count" : 1 }
    { "_id" : [ "races" ], "count" : 1 }
    { "_id" : [ "gta", "distribution" ], "count" : 1 }
    { "_id" : [ "samp" ], "count" : 1 }
    { "_id" : [ "distribution", "keys" ], "count" : 1 }
    { "_id" : [ "gta" ], "count" : 3 }
    { "_id" : [ "online" ], "count" : 2 }
    { "_id" : [ "keys" ], "count" : 1 }
    { "_id" : [ "gta", "online" ], "count" : 2 }
    { "_id" : [ "moto" ], "count" : 1 }
    { "_id" : [ "online", "moto" ], "count" : 1 }
    { "_id" : [ "distribution" ], "count" : 1 }
    { "_id" : [ "gta", "moto" ], "count" : 1 }
    { "_id" : [ "gta", "races" ], "count" : 1 }
    

    如果您需要更多组合,您可能需要更新上面的$reduce 阶段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      相关资源
      最近更新 更多