总结mongodb中关联数组的值

时间:2017-05-09 13:03:47

标签: mongodb aggregation-framework

我在php中有一个关联数组,如

$aData['key1']['key2']['key4'] = value1;
$aData['key1']['key2']['key5'] = value2;
$aData['key1']['key2']['key6'] = value3;

我将这个数组保存到mongodb集合中

{    
"_id": ObjectID(),
"client_id": NumberLong(123),
"time": ISODate("..."),
"data": {
    "key1": {
        "key2": {
            "key4": value1,
            "key5": value2,
            "key6": value3
        },
        ...
    },
    ...
},
{    
"_id": ObjectID(),
"client_id": NumberLong(123),
"time": ISODate("..."),
"data": {
    "key1": {
        "key2": {
            "key4": value1,
            "key5": value2,
            "key6": value3
        },
        ...
    },
    ...
},
{...}

如何为给定的client_id和时间总结key1和key2的key4,key5,key6的值?

我试过了:

db.getCollection('...').aggregate( [ 
    {$match: {client_id: 11}}, 
    {$project: {'_id': 1, 'client_id': 1,'data.key1': 1}}, 
    {$group: {_id: '$client_id', sum: {$sum: "$data.key1.key2.key4"}}} 
]) 

但总和为0。

0 个答案:

没有答案