【问题标题】:Mongodb aggregate to sort items with most common items in array?Mongodb聚合以对数组中最常见的项目进行排序?
【发布时间】:2020-11-02 17:36:12
【问题描述】:

集合中的一个项目包含一个字符串数组。我想查找数组中匹配元素最多的项目并对其进行排序。

考虑一个集合:

[
    {
        "item_name":"Item_1",
        "tags":["A","B","C","D","E"]
    },
    {
        "item_name":"Item_2",
        "tags":["A","B","D","E","G"]
    },
    {
        "item_name":"Item_3",
        "tags":["B","C","E","H"]
    }

]

我想根据 ["B","D","G","F"] 之类的数组对集合进行排序 这将返回

[

    {
        "item_name":"Item_2",
        "tags":["A","B","D","E","G"]
    },
    {
        "item_name":"Item_1",
        "tags":["A","B","C","D","E"]
    },
    {
        "item_name":"Item_3",
        "tags":["B","C","E","H"]
    }

]

预期的顺序是 Item_2,Item_1,然后是 Item_3,

  • Item_2 匹配 3 个项目(“B”、“D”和“G”)
  • 然后 Item_1 有 2 个匹配项(“B”和“D”)
  • 最后是 Item_3 与 1 个匹配项 ("B")

如果不在 mongodb 中,JavaScript 方法也会受到赞赏

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    您可以使用$setIntersection 获取相交项目的数量,然后根据计算的分数使用$sort

    db.collection.aggregate([
      {
        $project: {
          _id: 0,
          item_name: "$item_name",
          tags: "$tags",
          score: {
            $let: {
              vars: {
                intersection: {
                  $setIntersection: [
                    "$tags",
                    [
                      "B",
                      "D",
                      "G",
                      "F"
                    ]
                  ]
                }
              },
              in: {
                $size: "$$intersection"
              }
            }
          }
        }
      },
      {
        $sort: {
          score: -1
        }
      }
    ])
    

    https://mongoplayground.net/p/tpDTtVKetFT

    【讨论】:

      【解决方案2】:

      如果tags 中的值是唯一的,则可以使用tags 的交集大小和使用$size$setIntersection 的查询数组

      db.collection.aggregate([
        {
          $set: {
            matchedCount: {
              $size: {
                $setIntersection: ["$tags", ["B","D","G","F"]]
              }
            }
          }
        },
        {
          $sort: {
            matchedCount: -1
          }
        }
      ])
      

      【讨论】:

      • 谢谢。这是准确的。您能否建议如何丢弃与至少 1 个标签不匹配的项目
      • 假设您正在搜索 ["B","D","G","F"],当您说“不匹配至少 1 个标签的项目”时,这是否意味着 ["A","D","G","F"]["B","D","G","F","H"] 或 ["D"," G","F"]?
      • 我的意思是 ["H","K","L"]
      • 您可以在管道的开头添加{ $match: { tags: { $in: ["B","D","G","F"] } } }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2019-12-26
      • 1970-01-01
      • 2020-03-31
      • 2020-09-10
      • 1970-01-01
      • 2016-02-01
      相关资源
      最近更新 更多