MongoDB聚合组,其元素名称由数据定义

时间:2014-08-01 06:30:59

标签: mongodb aggregation-framework

我试图在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"}}
    }
}

有没有办法做到这一点?

0 个答案:

没有答案