-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1654: Support new $concatArrays
and $setUnion
aggregation accumulators
#1732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# $schema: ../schema.json | ||
name: $concatArrays | ||
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatArrays/' | ||
type: | ||
- accumulator | ||
- window | ||
- resolvesToArray | ||
encode: single | ||
description: | | ||
Concatenates arrays to return the concatenated array. | ||
arguments: | ||
- | ||
name: array | ||
type: | ||
- resolvesToArray # of arrays | ||
variadic: array | ||
description: | | ||
An array of expressions that resolve to an array. | ||
If any argument resolves to a value of null or refers to a field that is missing, `$concatArrays` returns `null`. | ||
tests: | ||
- | ||
name: 'Warehouse collection' | ||
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatArrays/#example' | ||
pipeline: | ||
- | ||
$project: | ||
items: | ||
$concatArrays: | ||
- '$instock' | ||
- '$ordered' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# $schema: ../schema.json | ||
name: $setUnion | ||
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/setUnion/' | ||
type: | ||
- accumulator | ||
- window | ||
- resolvesToArray | ||
encode: single | ||
description: | | ||
Takes two or more arrays and returns an array containing the elements that appear in any input array. | ||
arguments: | ||
- | ||
name: array | ||
type: | ||
- resolvesToArray # of arrays | ||
variadic: array | ||
description: | | ||
An array of expressions that resolve to an array. | ||
tests: | ||
- | ||
name: 'Flowers collection' | ||
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/setUnion/#example' | ||
pipeline: | ||
- | ||
$project: | ||
flowerFieldA: 1 | ||
flowerFieldB: 1 | ||
allValues: | ||
$setUnion: | ||
- "$flowerFieldA" | ||
- "$flowerFieldB" | ||
_id: 0 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Tests\Builder\Accumulator; | ||
|
||
use MongoDB\Builder\Accumulator; | ||
use MongoDB\Builder\Expression; | ||
use MongoDB\Builder\Pipeline; | ||
use MongoDB\Builder\Stage; | ||
use MongoDB\Tests\Builder\PipelineTestCase; | ||
|
||
/** | ||
* Test $concatArrays accumulator | ||
*/ | ||
class ConcatArraysAccumulatorTest extends PipelineTestCase | ||
{ | ||
public function testWarehouseCollection(): void | ||
{ | ||
$concatArrays = Accumulator::concatArrays(...); | ||
|
||
$pipeline = new Pipeline( | ||
Stage::project( | ||
items: $concatArrays( | ||
Expression::arrayFieldPath('instock'), | ||
Expression::arrayFieldPath('ordered'), | ||
), | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::ConcatArraysWarehouseCollection, $pipeline); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Tests\Builder\Accumulator; | ||
|
||
use MongoDB\Builder\Accumulator; | ||
use MongoDB\Builder\Expression; | ||
use MongoDB\Builder\Pipeline; | ||
use MongoDB\Builder\Stage; | ||
use MongoDB\Tests\Builder\PipelineTestCase; | ||
|
||
/** | ||
* Test $setUnion accumulator | ||
*/ | ||
class SetUnionAccumulatorTest extends PipelineTestCase | ||
{ | ||
public function testFlowersCollection(): void | ||
{ | ||
$setUnion = Accumulator::setUnion(...); | ||
|
||
$pipeline = new Pipeline( | ||
Stage::project( | ||
flowerFieldA: 1, | ||
flowerFieldB: 1, | ||
allValues: $setUnion( | ||
Expression::arrayFieldPath('flowerFieldA'), | ||
Expression::arrayFieldPath('flowerFieldB'), | ||
), | ||
_id: 0, | ||
), | ||
); | ||
|
||
$this->assertSamePipeline(Pipelines::SetUnionFlowersCollection, $pipeline); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, our code from discussing different ways to write agg pipelines found its way in here. I'll let you decide whether you want to keep it this way or inline it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I liked this :)