MongoDB使用聚合转换数据

MongoDB使用聚合转换数据,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我收集了以下数据 [ {name: "a", status: 1}, {name: "a", status: 2}, {name: "a", status: 3}, {name: "b", status: 1}, {name: "b", status: 4}, {name: "c", status: 1}, {name: "c", status: 1}, {name: "c", status: 5} ] 我希望将其转换为: {

我收集了以下数据

[
    {name: "a", status: 1},
    {name: "a", status: 2},
    {name: "a", status: 3},
    {name: "b", status: 1},
    {name: "b", status: 4},
    {name: "c", status: 1},
    {name: "c", status: 1},
    {name: "c", status: 5}
]
我希望将其转换为:

{
    names: ["a", "b", "c"],
    statuses: [1, 2, 3, 4, 5],
    count: [
        [1, 1, 1, 0, 0],
        [1, 0, 0, 1, 0],
        [2, 0, 0, 0, 1]
    ]
}
说明:

  • 名称将具有唯一的名称列表属性
  • 状态将具有唯一的状态属性列表
  • Count包含每个名称的状态计数数组
  • 例如,第一个计数数组是[1,1,1,0,0]
  • 这意味着:
  • 只有一个状态:名称a为1
  • 只有一种状态:名称a为2,依此类推
  • 如果使用第三个数组[2,0,0,0,1]
  • 名称c有两次状态:1
  • 没有2、3或4,但有一个5
  • 您能推荐相应的聚合函数或等效函数吗

    编辑: 到目前为止,我只知道:

    db.collection1.aggregate([
      {
        $group: {
          _id: {environment: "$name", status: "$status"}, 
          count:{$sum: 1}
        }
      }
    ])
    

    谢谢

    很好!现在你能展示一下你到目前为止都做了些什么吗。。聚合([{$group:{{u id:{name:$name,status:$status},count:{$sum:1}}}])不要在这里发布代码。在你的问题上使用。我把它添加到问题中了好!现在你能展示一下你到目前为止都做了些什么吗。。聚合([{$group:{{u id:{name:$name,status:$status},count:{$sum:1}}}])不要在这里发布代码。在你的问题上使用。我把它添加到了问题中