MongoDB 中的 Lodash `countBy` 等价物?

Lodash `countBy` equivalent in MongoDB?

假设我有以下输入文档:

[
  {
    "_id": "6225ca4052e7c226e2dd836d",
    "data": [
      "07",
      "07",
      "12",
      "19",
      "07",
      "32"
    ]
  },
  {
    "_id": "6225ca4052e7c226e2dd888f",
    "data": [
      "99",
      "97",
      "52",
      "99",
      "58",
      "92"
    ]
  }
]

我想计算 data 字符串数组 每个文档 中每个元素的出现次数。在 JS 中,我可以使用 countBy。如何使用 MongoDB 聚合框架实现相同的目的?

我试过 $reduce 但 MongoDB 似乎不支持将动态字段分配给对象。

{
  $reduce: {
    input: '$data',
    initialValue: {},
    in: { // assign `$$this` with count to `$$value`, but failed! }
  }
}

下面是所需的输出。

[
  {
    "_id": "6225ca4052e7c226e2dd836d",
    "freqs": {
      "12": 1,
      "19": 1,
      "32": 1,
      "07": 3
    }
  },
  {
    "_id": "6225ca4052e7c226e2dd888f",
    "freqs": {
      "52": 1,
      "58": 1,
      "92": 1,
      "97": 1,
      "99": 2
    }
  }
]
db.collection.aggregate([
  {
    $match: {}
  },
  {
    $unwind: "$data"
  },
  {
    $group: {
      _id: "$data",
      c: { $sum: 1 },
      id: { $first: "$_id" }
    }
  },
  {
    $group: {
      _id: "$id",
      data: { $push: { k: "$_id", v: "$c" } }
    }
  },
  {
    $set: {
      data: { $arrayToObject: "$data" }
    }
  }
])

mongoplayground


db.collection.aggregate([
  {
    $set: {
      data: {
        $function: {
          body: "function(d) {let obj = {}; d.forEach(e => {if(obj[e]==null) { obj[e]=1; }else{ obj[e]++; }}); return obj;}",
          args: [
            "$data"
          ],
          lang: "js"
        }
      }
    }
  }
])

mongoplayground