I am trying to do an aggregation using this array:
[
[
'$match' => ['deck_id' => 18],
],
[
'$project' => [
'stack' => 1,
]
],
[
'$unwind' => '$stack'
],
[
'$group' => [
'_id' => '$stack.response',
'responses' => [
'$push' => '$$ROOT.stack'
]
]
],
[
'$project' => [
'responses_count' => ['$size' => '$responses']
]
]
]
But I am getting the following exception: Unrecognized pipeline stage name: '$unwind'
I also created an issue in GitHub with this error. I am using mongodb 4 and the latest mongodb extension from pecl.
https://github.com/mongodb/mongo-php-driver/issues/935
The following query works without error on Mongo Clients for example, but not in PHP:
db.getCollection('game_statistics').aggregate([
{$match: {deck_id: 18}},
{$project: {stack: 1}},
{$unwind: "$stack"},
{$group: {_id: "$stack.response", responses: {$push: "$$ROOT.stack"}}},
{$project: {responses_count: {'$size': "$responses"}}}
])
The problem was on my side. Even though the code was correct, there were a series of invisible UTF8 chars in the array keys or values that the driver was taking into account. Opening that file with VIM allowed me to see those characters and removing them worked. https://jira.mongodb.org/browse/PHPLIB-396