Marco Böhrs
Marco Böhrs

Reputation: 51

Field is missing in MongoDB Aggregation $group result in PHP, but not in Compass

I try to group documents by two fields, 'a' and 'b', to get all distinct pairs of them in the collection. It works in Compass, but not in PHP 7.2. Here my code:

$pipeline = [
        ['$group' => 
            ['_id' => 
                ['a' => '$a',
                'b' => '$b']
            ]
        ]
    ];

$collection->aggregate($pipeline);

What i get is:

Array ( [_id] => Array ( [a] => 1 ) )

Array ( [_id] => Array ( [a] => 2 ) )

Array ( [_id] => Array ( [a] => 3 ) )

Array ( [_id] => Array ( [a] => 4 ) )

Array ( [_id] => Array ( [a] => 5 ) )

Field 'b' is missing.

Upvotes: 0

Views: 283

Answers (1)

Marco Böhrs
Marco Böhrs

Reputation: 51

Recreating the collection solved the problem.

Upvotes: 1

Related Questions