我试图在Mongo中聚合以下内容:
{"customer_id":1,"attribute":"test1","value":101}
{"customer_id":1,"attribute":"test2","value":102}
{"customer_id":2,"attribute":"test1","value":201}
{"customer_id":2,"attribute":"test2","value":202}
为:
{
"customer_id":1,
"attributes":
{
"test1"::101,
"test2"::102
}
}
{
"customer_id":2,
"attributes":
{
"test1"::201,
"test2"::202
}
}
我使用$ group和$ push非常接近,但仍然会创建一个数组而不是关联数组。
{
"$group": {
"_id": "$customer_id",
"attributes": {"$push":{"attribute":"$attribute","value":"$value"}}
}
}
有没有办法做到这一点?