mongo 聚合 - 一个数组中的字段也在另一个数组中的文档数

mongo aggregation - number of documents where field in one array is also in another one

我有一部电影collection

...
{
    ...
    "cast":[ "First Actor", "Second Actor" ],
    "directors":[ "First Director", "Second Director" ]
},
{
    ...
    "cast": [ "Actor Director", "First Actor" ],
    "directors": [ "Actor Director", "Firt Director" ]
}
...

使用聚合框架,我需要获取文档数量,其中 directors 数组中的至少一个值也在 cast 数组中。我怎样才能实现它?

您可以使用 $setIntersection to find common entries in both arrays, then filter documents by $size of the result gt than 0 (means that at least one element is common to arrays), and finally use $count 阶段来统计符合此条件的文档。

-- 编辑:添加 $addFields 舞台以防演员或导演不存在数组

如果任何文档不包含 cast 或 directors 数组,您将收到 size 等待数组并获取空值的错误。 为了避免这种情况,您需要添加一个 $addField 阶段来为演员和导演定义空数组而不是 null。

查询如下:

db.collection.aggregate([
  {
    $addFields: {
      directors: {
        $cond: {
          if: {
            $isArray: "$directors"
          },
          then: "$directors",
          else: []
        }
      },
      cast: {
        $cond: {
          if: {
            $isArray: "$cast"
          },
          then: "$cast",
          else: []
        }
      }
    }
  },
  {
    $match: {
      $expr: {
        $gt: [
          {
            $size: {
              $setIntersection: [
                "$cast",
                "$directors"
              ]
            }
          },
          0
        ]
      }
    }
  },
  {
    $count: "have_common_value"
  }
])

你可以测试一下here