聚合:我如何计算该值为真值的键数?

发布于 2025-01-31 08:39:33 字数 667 浏览 2 评论 0原文

我在MongoDB中有很多文档,看起来像这样:

[
  {
    name: "steve",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  },
  {
    name: "john",
    accountFeatures: {word: false, excel: true, powerpoint: true, notes: true}
  },
  {
    name: "rick",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  }
]

我想运行一个聚合以找出对象中的每个键都设置为true(实际数据中还有更多键),所以样本数据的预期输出将是:

  {
    "res": {
      "excel": 1,
      "notes": 1,
      "powerpoint": 3,
      "word": 2
    }
  }

这可能吗?到目前为止,我所能做的就是这样:

[
  {
    '$project': {
      '_id': 0, 
      'accountFeatures': 1
  }
]

I have a load of documents in mongodb which look something like this:

[
  {
    name: "steve",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  },
  {
    name: "john",
    accountFeatures: {word: false, excel: true, powerpoint: true, notes: true}
  },
  {
    name: "rick",
    accountFeatures: {word: true, excel: false, powerpoint: true}
  }
]

I want to run an aggregation to find out how many of each of the keys in the objects are set to true (there are many more keys in the actual data), so the expected output for the sample data would be:

  {
    "res": {
      "excel": 1,
      "notes": 1,
      "powerpoint": 3,
      "word": 2
    }
  }

Is this possible? So far all I can think to do is this:

[
  {
    '$project': {
      '_id': 0, 
      'accountFeatures': 1
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2025-02-07 08:39:33

您可以使用$ objectToArray$ unvind,它将允许我们$ group by the键:

db.collection.aggregate([
  {
    $set: {accountFeatures: {$objectToArray: "$accountFeatures"}}
  },
  {
    $project: {
      accountFeatures: {
        $filter: {
          input: "$accountFeatures",
          as: "item",
          cond: {$eq: ["$item.v", true]}
        }
      }
    }
  },
  {
    $unwind: "$accountFeatures"
  },
  {
    $group: {_id: "$accountFeatures.k", v: {$sum: 1}}
  },
  {
    $group: {_id: 0, data: {$push: {k: "$_id", v: "$v"}}}
  },
  {
    $project: {res: {$arrayToObject: "$data"}, _id: 0}
  }
])
  1. $ objecttoArray提取密钥
  2. $ filter仅保留true ands
  3. $ UNTIND将键与不同文档的键
  4. $ group将计算每个键
  5. 格式的结果
    游乐场示例

You can use $objectToArray and $unwind which will allow us $group by the keys:

db.collection.aggregate([
  {
    $set: {accountFeatures: {$objectToArray: "$accountFeatures"}}
  },
  {
    $project: {
      accountFeatures: {
        $filter: {
          input: "$accountFeatures",
          as: "item",
          cond: {$eq: ["$item.v", true]}
        }
      }
    }
  },
  {
    $unwind: "$accountFeatures"
  },
  {
    $group: {_id: "$accountFeatures.k", v: {$sum: 1}}
  },
  {
    $group: {_id: 0, data: {$push: {k: "$_id", v: "$v"}}}
  },
  {
    $project: {res: {$arrayToObject: "$data"}, _id: 0}
  }
])
  1. $objectToArray to extract the keys
  2. $filter to keep only the true ones
  3. $unwind to separate the keys to different documents
  4. $group to count each key
  5. format the results
    Playground example
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文