Hi, i am getting this error when i try to use aggregate and i am new to mongoDB. please help me out.
MongoDB shell version: V3.0.12
jenssegers/mongodb v3.0.2
mongodb/mongodb V1.0.2
$statements = $this->db->statements->aggregate(
[
'$match' => $match
],
[
'$project' => [
'year' => ['$year' => '$timestamp'],
'month' => ['$month' => '$timestamp'],
'day' => ['$dayOfMonth' => '$timestamp'],
'actor' => '$statement.actor'
]
],
[
'$group' => [
'_id' => [
'year' => '$year',
'month' => '$month',
'day' => '$day'
],
'count' => ['$sum' => 1],
'actors' => ['$addToSet' => '$actor']
]
],
[
'$sort' => ['_id' => 1]
],
[
'$project' => [
'a' => '$count',
'b' => ['$size' => '$actors'],
'y' => ['$concat' => [
[ '$substr' => [ '$_id.year', 0, 4 ] ],
'-',
[ '$cond' => [
[ '$lte' => [ '$_id.month', 9 ] ],
[ '$concat' => [
'0',
[ '$substr' => [ '$_id.month', 0, 2 ] ],
]],
[ '$substr' => [ '$_id.month', 0, 2 ] ]
]],
'-',
[ '$cond' => [
[ '$lte' => [ '$_id.day', 9 ] ],
[ '$concat' => [
'0',
[ '$substr' => [ '$_id.day', 0, 2 ] ],
]],
[ '$substr' => [ '$_id.day', 0, 2 ] ]
]]
]
],
]
]
);
[2016-07-01 09:16:17] local.ERROR: MongoDB\Exception\InvalidArgumentException: $pipeline is not a list (unexpected index: "$match") in /home/vagrant/Code/newlearning/vendor/mongodb/mongodb/src/Operation/Aggregate.php:94
Stack trace:
Wrapping the aggregation in an array should work.
$statements = $this->db->statements->aggregate([
[
'$match' => $match
],
[
'$project' => [
'year' => ['$year' => '$timestamp'],
'month' => ['$month' => '$timestamp'],
'day' => ['$dayOfMonth' => '$timestamp'],
'actor' => '$statement.actor'
]
],
[
'$group' => [
'_id' => [
'year' => '$year',
'month' => '$month',
'day' => '$day'
],
'count' => ['$sum' => 1],
'actors' => ['$addToSet' => '$actor']
]
],
[
'$sort' => ['_id' => 1]
],
[
'$project' => [
'a' => '$count',
'b' => ['$size' => '$actors'],
'y' => [
'$concat' => [
['$substr' => ['$_id.year', 0, 4]],
'-',
[
'$cond' => [
['$lte' => ['$_id.month', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.month', 0, 2]],
]
],
['$substr' => ['$_id.month', 0, 2]]
]
],
'-',
[
'$cond' => [
['$lte' => ['$_id.day', 9]],
[
'$concat' => [
'0',
['$substr' => ['$_id.day', 0, 2]],
]
],
['$substr' => ['$_id.day', 0, 2]]
]
]
]
],
]
]
]
);
thanks @kevinem, now this working....
Most helpful comment
Wrapping the aggregation in an array should work.