diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 0dbf6e9..2f15bff 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -120,13 +120,13 @@ public function findAll(): self $pipeline = []; } - if (!empty($this->lookup)) // Lookups should ALWAYS be the first ones - { - $pipeline[] = $this->lookup; + // Lookups should ALWAYS be the first ones + if (!empty($this->lookup)) { + $pipeline = array_merge($pipeline, $this->lookup); } if (!empty($this->unwind)) { - $pipeline[] = $this->unwind; + $pipeline = array_merge($pipeline, $this->unwind); } if (!empty($this->addFields)) { @@ -423,7 +423,7 @@ public function join($collection, $localField, $operatorOrForeignField, $foreign ] ]; - $this->lookup = [ + $this->lookup[] = [ "\$lookup" => [ "from" => key($collection), "let" => [reset($foreignField) => "\$" . $localField], @@ -432,7 +432,7 @@ public function join($collection, $localField, $operatorOrForeignField, $foreign ], ]; - $this->unwind = [ + $this->unwind[] = [ "\$unwind" => ["path" => "\$" . reset($collection)] ];