-
Type: New Feature
-
Resolution: Fixed
-
Priority: Unknown
-
Affects Version/s: None
-
Component/s: None
-
None
- Accept a MongoDB\Builder\Pipeline as argument for DB::aggregate, Collection::aggregate, Client::watch, DB::watch, and Collection::watch methods
- Pipeline is encoded to BSON using the encoder provided in the client/database/collection options
Since we cannot change the type of the $pipeline argument in the mentioned methods, we can only accept a list of Stage objects. In the version 1.20+, the code look like this:
$pipeline = new Pipeline( Stage::match(...), Stage::limit(...), ); // Required for now, as we could not change the argument type. In 2.0 this will not be necessary. $pipeline = iterator_to_array($pipeline); $pipeline = (new CustomBuilderEncoder)->encode($pipeline); $collection->aggregate($pipeline);
It's possible to enable the new behavior in a class extending MongoDB\Collection:
use MongoDB\Builder\Pipeline; use MongoDB\Collection; class CustomCollection extends Collection { /** @param array|Pipeline $pipeline */ public function aggregate(array|Pipeline $pipeline = [], array $options = []) { // Enable behavior of version 2.0: Pipeline objects are accepted if ($pipeline instanceof Pipeline) { $pipeline = iterator_to_array($pipeline); } return parent::aggregate($pipeline, $options); } /** @param array|Pipeline $pipeline */ public function watch(array|Pipeline $pipeline = [], array $options = []) { // Enable behavior of version 2.0: Pipeline objects are accepted if ($pipeline instanceof Pipeline) { $pipeline = iterator_to_array($pipeline); } return parent::watch($pipeline, $options); } }
- is duplicated by
-
PHPLIB-1286 Handle Pipeline&Decoder in Collection::aggregate()
- Closed