从聚合管道中的数组中提取唯一值

Extract unique values from Array in aggregation pipeline

在我的聚合管道中,经过一些先前的聚合,我最终得到类似于这些的文档:

[
  {
    "_id": 0,
    "group": "Electronics",
    // other fields omitted for brevity
    "articles": [
      {
        "name": "Gameboy",
        // Even more fields omitted for brevity
        "area": "Video Games"
      },
      {
        "name": "Playstation",
        "area": "Video Games"
      },
      {
        "name": "Refrigerator",
        "area": "White Goods"
      }
    ]
  },
  {
    "_id": 1,
    "group": "Food",
    "articles": [
      {
        "name": "Apple",
        "area": "Fruit"
      },
      {
        "name": "Pear",
        "area": "Fruit"
      }
    ]
  }
]

我需要从数组中提取唯一的 area 值,同时保持文档的其余部分完整(不过之后不需要 articles)。结果应如下所示:


[
    {
        "_id": 0,
        "group": "Electronics",
        // other fields...
        "articleAreas": [ "Video Games", "White Goods" ]
    },
    {
        "_id": 1,
        "group": "Food",
        "articleAreas": [ "Fruit" ]
    }
]

我的直觉是应该有 一些 方法来使用 $addToSet 类似于在 $group 阶段可以完成的事情,但我不能'不知道怎么弄。

您可以尝试 $addFields 阶段和 $setUnion 运算符从数组中获取唯一值,

  • $addFields 添加新字段 articleAreas
  • $setUnionarticles.area
  • 的值数组中获取唯一值
db.collection.aggregate([
  {
    $addFields: {
      articleAreas: {
        $setUnion: "$articles.area"
      }
    }
  }
])

Playground