Skip to content

Commit 5f6ab18

Browse files
committed
Create setUnion accumulator
1 parent aa90304 commit 5f6ab18

File tree

7 files changed

+252
-0
lines changed

7 files changed

+252
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# $schema: ../schema.json
2+
name: $setUnion
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/setUnion/'
4+
type:
5+
- accumulator
6+
- window
7+
- resolvesToArray
8+
encode: single
9+
description: |
10+
Takes two or more arrays and returns an array containing the elements that appear in any input array.
11+
arguments:
12+
-
13+
name: array
14+
type:
15+
- resolvesToArray # of arrays
16+
variadic: array
17+
description: |
18+
An array of expressions that resolve to an array.
19+
tests:
20+
-
21+
name: 'Flowers collection'
22+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/setUnion/#example'
23+
pipeline:
24+
-
25+
$project:
26+
flowerFieldA: 1
27+
flowerFieldB: 1
28+
allValues:
29+
$setUnion:
30+
- "$flowerFieldA"
31+
- "$flowerFieldB"
32+
_id: 0

src/Builder/Accumulator/ConcatArraysAccumulator.php

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/FactoryTrait.php

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Accumulator/SetUnionAccumulator.php

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Accumulator;
6+
7+
use MongoDB\Builder\Accumulator;
8+
use MongoDB\Builder\Expression;
9+
use MongoDB\Builder\Pipeline;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $concatArrays accumulator
15+
*/
16+
class ConcatArraysAccumulatorTest extends PipelineTestCase
17+
{
18+
public function testWarehouseCollection(): void
19+
{
20+
$concatArrays = Accumulator::concatArrays(...);
21+
22+
$pipeline = new Pipeline(
23+
Stage::project(
24+
items: $concatArrays(
25+
Expression::arrayFieldPath('instock'),
26+
Expression::arrayFieldPath('ordered'),
27+
),
28+
),
29+
);
30+
31+
$this->assertSamePipeline(Pipelines::ConcatArraysWarehouseCollection, $pipeline);
32+
}
33+
}

tests/Builder/Accumulator/Pipelines.php

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MongoDB\Tests\Builder\Accumulator;
6+
7+
use MongoDB\Builder\Accumulator;
8+
use MongoDB\Builder\Expression;
9+
use MongoDB\Builder\Pipeline;
10+
use MongoDB\Builder\Stage;
11+
use MongoDB\Tests\Builder\PipelineTestCase;
12+
13+
/**
14+
* Test $setUnion accumulator
15+
*/
16+
class SetUnionAccumulatorTest extends PipelineTestCase
17+
{
18+
public function testFlowersCollection(): void
19+
{
20+
$setUnion = Accumulator::setUnion(...);
21+
22+
$pipeline = new Pipeline(
23+
Stage::project(
24+
flowerFieldA: 1,
25+
flowerFieldB: 1,
26+
allValues: $setUnion(
27+
Expression::arrayFieldPath('flowerFieldA'),
28+
Expression::arrayFieldPath('flowerFieldB'),
29+
),
30+
_id: 0,
31+
),
32+
);
33+
34+
$this->assertSamePipeline(Pipelines::SetUnionFlowersCollection, $pipeline);
35+
}
36+
}

0 commit comments

Comments
 (0)