我在集合中有以下数据。
[
{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]
]
}
说明:
您能否建议相应的聚合函数或等效函数?
编辑: 我到目前为止只是这样:
db.collection1.aggregate([
{
$group: {
_id: {environment: "$name", status: "$status"},
count:{$sum: 1}
}
}
])
谢谢