我对 Mongo 完全陌生,我在围绕聚合框架时遇到了一些严重的麻烦。我有一个看起来像这样的文档:
record {
_id: '2013-04-22/tacos',
first: {
something: {0: 1, 1:4},
somethingelse: {0: 18, 1:22}
},
second: {
0: [11, 18, 7],
1: [17, 9, 22]
},
metadata: {
date: '2013-04-22'
}
}
我想要做的是获取 first.something 中每个项目的总和(因为这将需要处理多个文档),第二个中的每个项目,然后是first.something
, second.<whatever>.0
,second.<whatever>.1
和中所有项目的总和second.<whatever>.2
。所以我正在寻找这样的结果(请原谅我的格式):
sum(first.something.0): 1
sum(first.something.1): 4
sum(second.0.0): 11
sum(second.0.1): 18
sum(second.0.2): 7
sum(second.1.0): 17
sum(second.1.1): 9
sum(second.1.2): 22
sum(first.something.all): 5
sum(second.<whatever>.0): 28
sum(second.<whatever>.1): 27
sum(second.<whatever>.2): 29
现在我只是想让前两个工作,在一个记录上,我有这个:
// $db is my collection, and is verified to work
$aggregate = array(
'$group' => array(
'_id' => '$metadata.date',
'first_zero' => array('$sum' => 'first.something.0'),
'first_one' => array('$sum' => 'first.something.1')
),
'$match' => array(
"metadata.date" => '2013-04-22'
)
);
$cursor = $db->aggregate($aggregate);
var_dump($cursor);
但是我的输出告诉我exception: A pipeline stage specification object must contain exactly one field.
。但是,如果我省略了该'$match'
部分,它会返回带有 int(0) 的记录,first_zero
如下first_one
所示:
array(2) {
["result"]=>
array(1) {
[0]=>
array(3) {
["_id"]=>
string(10) "2013-04-22"
["clicks_zero"]=>
int(0)
["clicks_one"]=>
int(0)
}
}
["ok"]=>
float(1)
}
所以我什至无法让简单的部分工作。我在这里做错了什么?甚至是second.<whatever>.0
我能做的事情吗?