From 52c0b3b1a8a7a7057fa2efdbfe7f586ed176e93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Wed, 24 Jan 2024 17:39:08 +0100 Subject: [PATCH 1/3] PHPLIB-1340 Add tests on Projection Operators --- generator/config/projection/elemMatch.yaml | 27 ++++ generator/config/projection/filter.yaml | 34 ---- generator/config/projection/slice.yaml | 38 +++++ src/Builder/Projection/FactoryTrait.php | 29 +--- src/Builder/Projection/SliceOperator.php | 9 +- tests/Builder/BuilderEncoderTest.php | 3 +- .../Projection/ElemMatchOperatorTest.php | 54 +++++++ tests/Builder/Projection/Pipelines.php | 146 ++++++++++++++++++ .../Builder/Projection/SliceOperatorTest.php | 60 +++++++ 9 files changed, 333 insertions(+), 67 deletions(-) delete mode 100644 generator/config/projection/filter.yaml create mode 100644 tests/Builder/Projection/ElemMatchOperatorTest.php create mode 100644 tests/Builder/Projection/SliceOperatorTest.php diff --git a/generator/config/projection/elemMatch.yaml b/generator/config/projection/elemMatch.yaml index 32f6ccd..d44499c 100644 --- a/generator/config/projection/elemMatch.yaml +++ b/generator/config/projection/elemMatch.yaml @@ -11,3 +11,30 @@ arguments: name: query type: - query +tests: + - + name: 'Zipcode Search' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#zipcode-search' + pipeline: + - + $match: + zipcode: '63109' + - + $project: + students: + $elemMatch: + school: 102 + - + name: 'with Multiple Fields' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#with-multiple-fields' + pipeline: + - + $match: + zipcode: '63109' + - + $project: + students: + $elemMatch: + school: 102 + age: + $gt: 10 diff --git a/generator/config/projection/filter.yaml b/generator/config/projection/filter.yaml deleted file mode 100644 index 50cfaae..0000000 --- a/generator/config/projection/filter.yaml +++ /dev/null @@ -1,34 +0,0 @@ -# $schema: ../schema.json -name: $filter -link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/' -type: - - projection -encode: object -description: | - Selects a subset of the array to return an array with only the elements that match the filter condition. -arguments: - - - name: input - type: - - resolvesToArray - - - name: cond - type: - - resolvesToBool - description: | - An expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the input array individually with the variable name specified in as. - - - name: as - type: - - string - optional: true - description: | - A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this. - - - name: limit - type: - - resolvesToInt - optional: true - description: | - A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array. - If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. If the limit is null, $filter returns all matching array elements. diff --git a/generator/config/projection/slice.yaml b/generator/config/projection/slice.yaml index cfab8f4..944265f 100644 --- a/generator/config/projection/slice.yaml +++ b/generator/config/projection/slice.yaml @@ -15,3 +15,41 @@ arguments: name: skip type: - int + optional: true +tests: + - + name: 'Return an Array with Its First 3 Elements' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-first-3-elements' + pipeline: + - + $project: + comments: + # Example uses the short form, the builder always generated the verbose form + # $slice: 3 + $slice: [3] + - + name: 'Return an Array with Its Last 3 Elements' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-last-3-elements' + pipeline: + - + $project: + comments: + # Example uses the short form, the builder always generated the verbose form + # $slice: -3 + $slice: [-3] + - + name: 'Return an Array with 3 Elements After Skipping the First Element' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-first-element' + pipeline: + - + $project: + comments: + $slice: [1, 3] + - + name: 'Return an Array with 3 Elements After Skipping the Last Element' + link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-last-element' + pipeline: + - + $project: + comments: + $slice: [-1, 3] diff --git a/src/Builder/Projection/FactoryTrait.php b/src/Builder/Projection/FactoryTrait.php index d3f1d23..1eddae0 100644 --- a/src/Builder/Projection/FactoryTrait.php +++ b/src/Builder/Projection/FactoryTrait.php @@ -8,13 +8,8 @@ namespace MongoDB\Builder\Projection; -use MongoDB\BSON\PackedArray; -use MongoDB\Builder\Expression\ResolvesToArray; -use MongoDB\Builder\Expression\ResolvesToBool; -use MongoDB\Builder\Expression\ResolvesToInt; use MongoDB\Builder\Type\Optional; use MongoDB\Builder\Type\QueryInterface; -use MongoDB\Model\BSONArray; /** * @internal @@ -32,34 +27,14 @@ public static function elemMatch(QueryInterface|array $query): ElemMatchOperator return new ElemMatchOperator($query); } - /** - * Selects a subset of the array to return an array with only the elements that match the filter condition. - * - * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/ - * @param BSONArray|PackedArray|ResolvesToArray|array $input - * @param ResolvesToBool|bool $cond An expression that resolves to a boolean value used to determine if an element should be included in the output array. The expression references each element of the input array individually with the variable name specified in as. - * @param Optional|string $as A name for the variable that represents each individual element of the input array. If no name is specified, the variable name defaults to this. - * @param Optional|ResolvesToInt|int $limit A number expression that restricts the number of matching array elements that $filter returns. You cannot specify a limit less than 1. The matching array elements are returned in the order they appear in the input array. - * If the specified limit is greater than the number of matching array elements, $filter returns all matching array elements. If the limit is null, $filter returns all matching array elements. - */ - public static function filter( - PackedArray|ResolvesToArray|BSONArray|array $input, - ResolvesToBool|bool $cond, - Optional|string $as = Optional::Undefined, - Optional|ResolvesToInt|int $limit = Optional::Undefined, - ): FilterOperator - { - return new FilterOperator($input, $cond, $as, $limit); - } - /** * Limits the number of elements projected from an array. Supports skip and limit slices. * * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/ * @param int $limit - * @param int $skip + * @param Optional|int $skip */ - public static function slice(int $limit, int $skip): SliceOperator + public static function slice(int $limit, Optional|int $skip = Optional::Undefined): SliceOperator { return new SliceOperator($limit, $skip); } diff --git a/src/Builder/Projection/SliceOperator.php b/src/Builder/Projection/SliceOperator.php index 5f62076..eef74aa 100644 --- a/src/Builder/Projection/SliceOperator.php +++ b/src/Builder/Projection/SliceOperator.php @@ -10,6 +10,7 @@ use MongoDB\Builder\Type\Encode; use MongoDB\Builder\Type\OperatorInterface; +use MongoDB\Builder\Type\Optional; use MongoDB\Builder\Type\ProjectionInterface; /** @@ -24,14 +25,14 @@ class SliceOperator implements ProjectionInterface, OperatorInterface /** @var int $limit */ public readonly int $limit; - /** @var int $skip */ - public readonly int $skip; + /** @var Optional|int $skip */ + public readonly Optional|int $skip; /** * @param int $limit - * @param int $skip + * @param Optional|int $skip */ - public function __construct(int $limit, int $skip) + public function __construct(int $limit, Optional|int $skip = Optional::Undefined) { $this->limit = $limit; $this->skip = $skip; diff --git a/tests/Builder/BuilderEncoderTest.php b/tests/Builder/BuilderEncoderTest.php index b280785..32ecce7 100644 --- a/tests/Builder/BuilderEncoderTest.php +++ b/tests/Builder/BuilderEncoderTest.php @@ -10,7 +10,6 @@ use MongoDB\Builder\BuilderEncoder; use MongoDB\Builder\Expression; use MongoDB\Builder\Pipeline; -use MongoDB\Builder\Projection; use MongoDB\Builder\Query; use MongoDB\Builder\Stage; use MongoDB\Builder\Variable; @@ -157,7 +156,7 @@ public function testExpressionFilter(array $limit, array $expectedLimit): void { $pipeline = new Pipeline( Stage::project( - items: Projection::filter( + items: Expression::filter( ...$limit, input: Expression::arrayFieldPath('items'), cond: Expression::gte(Expression::variable('item.price'), 100), diff --git a/tests/Builder/Projection/ElemMatchOperatorTest.php b/tests/Builder/Projection/ElemMatchOperatorTest.php new file mode 100644 index 0000000..a8e75f8 --- /dev/null +++ b/tests/Builder/Projection/ElemMatchOperatorTest.php @@ -0,0 +1,54 @@ +assertSamePipeline(Pipelines::ElemMatchWithMultipleFields, $pipeline); + } + + public function testZipcodeSearch(): void + { + $pipeline = new Pipeline( + Stage::match( + zipcode: '63109', + ), + Stage::project( + students: Projection::elemMatch( + Query::query( + school: 102, + ), + ), + ), + ); + + $this->assertSamePipeline(Pipelines::ElemMatchZipcodeSearch, $pipeline); + } +} diff --git a/tests/Builder/Projection/Pipelines.php b/tests/Builder/Projection/Pipelines.php index 251d9e6..2b86997 100644 --- a/tests/Builder/Projection/Pipelines.php +++ b/tests/Builder/Projection/Pipelines.php @@ -10,4 +10,150 @@ enum Pipelines: string { + /** + * Zipcode Search + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#zipcode-search + */ + case ElemMatchZipcodeSearch = <<<'JSON' + [ + { + "$match": { + "zipcode": "63109" + } + }, + { + "$project": { + "students": { + "$elemMatch": { + "school": { + "$numberInt": "102" + } + } + } + } + } + ] + JSON; + + /** + * with Multiple Fields + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#with-multiple-fields + */ + case ElemMatchWithMultipleFields = <<<'JSON' + [ + { + "$match": { + "zipcode": "63109" + } + }, + { + "$project": { + "students": { + "$elemMatch": { + "school": { + "$numberInt": "102" + }, + "age": { + "$gt": { + "$numberInt": "10" + } + } + } + } + } + } + ] + JSON; + + /** + * Return an Array with Its First 3 Elements + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-first-3-elements + */ + case SliceReturnAnArrayWithItsFirst3Elements = <<<'JSON' + [ + { + "$project": { + "comments": { + "$slice": [ + { + "$numberInt": "3" + } + ] + } + } + } + ] + JSON; + + /** + * Return an Array with Its Last 3 Elements + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-last-3-elements + */ + case SliceReturnAnArrayWithItsLast3Elements = <<<'JSON' + [ + { + "$project": { + "comments": { + "$slice": [ + { + "$numberInt": "-3" + } + ] + } + } + } + ] + JSON; + + /** + * Return an Array with 3 Elements After Skipping the First Element + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-first-element + */ + case SliceReturnAnArrayWith3ElementsAfterSkippingTheFirstElement = <<<'JSON' + [ + { + "$project": { + "comments": { + "$slice": [ + { + "$numberInt": "1" + }, + { + "$numberInt": "3" + } + ] + } + } + } + ] + JSON; + + /** + * Return an Array with 3 Elements After Skipping the Last Element + * + * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-last-element + */ + case SliceReturnAnArrayWith3ElementsAfterSkippingTheLastElement = <<<'JSON' + [ + { + "$project": { + "comments": { + "$slice": [ + { + "$numberInt": "-1" + }, + { + "$numberInt": "3" + } + ] + } + } + } + ] + JSON; } diff --git a/tests/Builder/Projection/SliceOperatorTest.php b/tests/Builder/Projection/SliceOperatorTest.php new file mode 100644 index 0000000..3ccc463 --- /dev/null +++ b/tests/Builder/Projection/SliceOperatorTest.php @@ -0,0 +1,60 @@ +assertSamePipeline(Pipelines::SliceReturnAnArrayWith3ElementsAfterSkippingTheFirstElement, $pipeline); + } + + public function testReturnAnArrayWith3ElementsAfterSkippingTheLastElement(): void + { + $pipeline = new Pipeline( + Stage::project( + comments: Projection::slice(-1, 3), + ), + ); + + $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWith3ElementsAfterSkippingTheLastElement, $pipeline); + } + + public function testReturnAnArrayWithItsFirst3Elements(): void + { + $pipeline = new Pipeline( + Stage::project( + comments: Projection::slice(3), + ), + ); + + $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWithItsFirst3Elements, $pipeline); + } + + public function testReturnAnArrayWithItsLast3Elements(): void + { + $pipeline = new Pipeline( + Stage::project( + comments: Projection::slice(-3), + ), + ); + + $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWithItsLast3Elements, $pipeline); + } +} From 2890017fa72472cb5dfae93ddffa5e2ee94403e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 30 Jan 2024 16:53:31 +0100 Subject: [PATCH 2/3] Remove $slice operator from projection, only allowed in find command --- generator/config/projection/slice.yaml | 55 ------------ src/Builder/Projection/FactoryTrait.php | 13 --- src/Builder/Projection/FilterOperator.php | 76 ---------------- src/Builder/Projection/SliceOperator.php | 45 ---------- tests/Builder/Projection/Pipelines.php | 90 ------------------- .../Builder/Projection/SliceOperatorTest.php | 60 ------------- 6 files changed, 339 deletions(-) delete mode 100644 generator/config/projection/slice.yaml delete mode 100644 src/Builder/Projection/FilterOperator.php delete mode 100644 src/Builder/Projection/SliceOperator.php delete mode 100644 tests/Builder/Projection/SliceOperatorTest.php diff --git a/generator/config/projection/slice.yaml b/generator/config/projection/slice.yaml deleted file mode 100644 index 944265f..0000000 --- a/generator/config/projection/slice.yaml +++ /dev/null @@ -1,55 +0,0 @@ -# $schema: ../schema.json -name: $slice -link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/' -type: - - projection -encode: array -description: | - Limits the number of elements projected from an array. Supports skip and limit slices. -arguments: - - - name: limit - type: - - int - - - name: skip - type: - - int - optional: true -tests: - - - name: 'Return an Array with Its First 3 Elements' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-first-3-elements' - pipeline: - - - $project: - comments: - # Example uses the short form, the builder always generated the verbose form - # $slice: 3 - $slice: [3] - - - name: 'Return an Array with Its Last 3 Elements' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-last-3-elements' - pipeline: - - - $project: - comments: - # Example uses the short form, the builder always generated the verbose form - # $slice: -3 - $slice: [-3] - - - name: 'Return an Array with 3 Elements After Skipping the First Element' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-first-element' - pipeline: - - - $project: - comments: - $slice: [1, 3] - - - name: 'Return an Array with 3 Elements After Skipping the Last Element' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-last-element' - pipeline: - - - $project: - comments: - $slice: [-1, 3] diff --git a/src/Builder/Projection/FactoryTrait.php b/src/Builder/Projection/FactoryTrait.php index 1eddae0..359926d 100644 --- a/src/Builder/Projection/FactoryTrait.php +++ b/src/Builder/Projection/FactoryTrait.php @@ -8,7 +8,6 @@ namespace MongoDB\Builder\Projection; -use MongoDB\Builder\Type\Optional; use MongoDB\Builder\Type\QueryInterface; /** @@ -26,16 +25,4 @@ public static function elemMatch(QueryInterface|array $query): ElemMatchOperator { return new ElemMatchOperator($query); } - - /** - * Limits the number of elements projected from an array. Supports skip and limit slices. - * - * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/ - * @param int $limit - * @param Optional|int $skip - */ - public static function slice(int $limit, Optional|int $skip = Optional::Undefined): SliceOperator - { - return new SliceOperator($limit, $skip); - } } diff --git a/src/Builder/Projection/FilterOperator.php b/src/Builder/Projection/FilterOperator.php deleted file mode 100644 index 3b29e28..0000000 --- a/src/Builder/Projection/FilterOperator.php +++ /dev/null @@ -1,76 +0,0 @@ -input = $input; - $this->cond = $cond; - $this->as = $as; - $this->limit = $limit; - } - - public function getOperator(): string - { - return '$filter'; - } -} diff --git a/src/Builder/Projection/SliceOperator.php b/src/Builder/Projection/SliceOperator.php deleted file mode 100644 index eef74aa..0000000 --- a/src/Builder/Projection/SliceOperator.php +++ /dev/null @@ -1,45 +0,0 @@ -limit = $limit; - $this->skip = $skip; - } - - public function getOperator(): string - { - return '$slice'; - } -} diff --git a/tests/Builder/Projection/Pipelines.php b/tests/Builder/Projection/Pipelines.php index 2b86997..e80cb34 100644 --- a/tests/Builder/Projection/Pipelines.php +++ b/tests/Builder/Projection/Pipelines.php @@ -66,94 +66,4 @@ enum Pipelines: string } ] JSON; - - /** - * Return an Array with Its First 3 Elements - * - * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-first-3-elements - */ - case SliceReturnAnArrayWithItsFirst3Elements = <<<'JSON' - [ - { - "$project": { - "comments": { - "$slice": [ - { - "$numberInt": "3" - } - ] - } - } - } - ] - JSON; - - /** - * Return an Array with Its Last 3 Elements - * - * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-its-last-3-elements - */ - case SliceReturnAnArrayWithItsLast3Elements = <<<'JSON' - [ - { - "$project": { - "comments": { - "$slice": [ - { - "$numberInt": "-3" - } - ] - } - } - } - ] - JSON; - - /** - * Return an Array with 3 Elements After Skipping the First Element - * - * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-first-element - */ - case SliceReturnAnArrayWith3ElementsAfterSkippingTheFirstElement = <<<'JSON' - [ - { - "$project": { - "comments": { - "$slice": [ - { - "$numberInt": "1" - }, - { - "$numberInt": "3" - } - ] - } - } - } - ] - JSON; - - /** - * Return an Array with 3 Elements After Skipping the Last Element - * - * @see https://www.mongodb.com/docs/manual/reference/operator/projection/slice/#return-an-array-with-3-elements-after-skipping-the-last-element - */ - case SliceReturnAnArrayWith3ElementsAfterSkippingTheLastElement = <<<'JSON' - [ - { - "$project": { - "comments": { - "$slice": [ - { - "$numberInt": "-1" - }, - { - "$numberInt": "3" - } - ] - } - } - } - ] - JSON; } diff --git a/tests/Builder/Projection/SliceOperatorTest.php b/tests/Builder/Projection/SliceOperatorTest.php deleted file mode 100644 index 3ccc463..0000000 --- a/tests/Builder/Projection/SliceOperatorTest.php +++ /dev/null @@ -1,60 +0,0 @@ -assertSamePipeline(Pipelines::SliceReturnAnArrayWith3ElementsAfterSkippingTheFirstElement, $pipeline); - } - - public function testReturnAnArrayWith3ElementsAfterSkippingTheLastElement(): void - { - $pipeline = new Pipeline( - Stage::project( - comments: Projection::slice(-1, 3), - ), - ); - - $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWith3ElementsAfterSkippingTheLastElement, $pipeline); - } - - public function testReturnAnArrayWithItsFirst3Elements(): void - { - $pipeline = new Pipeline( - Stage::project( - comments: Projection::slice(3), - ), - ); - - $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWithItsFirst3Elements, $pipeline); - } - - public function testReturnAnArrayWithItsLast3Elements(): void - { - $pipeline = new Pipeline( - Stage::project( - comments: Projection::slice(-3), - ), - ); - - $this->assertSamePipeline(Pipelines::SliceReturnAnArrayWithItsLast3Elements, $pipeline); - } -} From fd1761d7a3224373c83b73f619b3a20162202cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 30 Jan 2024 17:00:12 +0100 Subject: [PATCH 3/3] Remove Projection operators --- generator/config/definitions.php | 12 ---- generator/config/expression/slice.yaml | 1 - generator/config/expressions.php | 4 -- generator/config/projection/elemMatch.yaml | 40 ----------- generator/config/query/natural.yaml | 8 --- generator/config/schema.json | 2 - generator/config/stage/project.yaml | 1 - src/Builder/Expression/SliceOperator.php | 3 +- src/Builder/Query/FactoryTrait.php | 10 --- src/Builder/Query/NaturalOperator.php | 32 --------- src/Builder/Stage/FactoryTrait.php | 5 +- src/Builder/Stage/ProjectStage.php | 12 ++-- .../Projection/ElemMatchOperatorTest.php | 54 --------------- tests/Builder/Projection/Pipelines.php | 69 ------------------- 14 files changed, 7 insertions(+), 246 deletions(-) delete mode 100644 generator/config/projection/elemMatch.yaml delete mode 100644 generator/config/query/natural.yaml delete mode 100644 src/Builder/Query/NaturalOperator.php delete mode 100644 tests/Builder/Projection/ElemMatchOperatorTest.php delete mode 100644 tests/Builder/Projection/Pipelines.php diff --git a/generator/config/definitions.php b/generator/config/definitions.php index baf101e..ca3854c 100644 --- a/generator/config/definitions.php +++ b/generator/config/definitions.php @@ -56,16 +56,4 @@ OperatorTestGenerator::class, ], ], - - // Projection Operators - [ - 'configFiles' => __DIR__ . '/projection', - 'namespace' => 'MongoDB\\Builder\\Projection', - 'classNameSuffix' => 'Operator', - 'generators' => [ - OperatorClassGenerator::class, - OperatorFactoryGenerator::class, - OperatorTestGenerator::class, - ], - ], ]; diff --git a/generator/config/expression/slice.yaml b/generator/config/expression/slice.yaml index d0abbbb..22cc287 100644 --- a/generator/config/expression/slice.yaml +++ b/generator/config/expression/slice.yaml @@ -3,7 +3,6 @@ name: $slice link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/' type: - resolvesToArray - - projection encode: array description: | Returns a subset of an array. diff --git a/generator/config/expressions.php b/generator/config/expressions.php index 41cc3ec..76e54fe 100644 --- a/generator/config/expressions.php +++ b/generator/config/expressions.php @@ -89,10 +89,6 @@ 'returnType' => Type\QueryInterface::class, 'acceptedTypes' => [Type\QueryInterface::class, 'array'], ], - 'projection' => [ - 'returnType' => Type\ProjectionInterface::class, - 'acceptedTypes' => [Type\ProjectionInterface::class, ...$bsonTypes['object']], - ], 'accumulator' => [ 'returnType' => Type\AccumulatorInterface::class, 'acceptedTypes' => [Type\AccumulatorInterface::class, ...$bsonTypes['object']], diff --git a/generator/config/projection/elemMatch.yaml b/generator/config/projection/elemMatch.yaml deleted file mode 100644 index d44499c..0000000 --- a/generator/config/projection/elemMatch.yaml +++ /dev/null @@ -1,40 +0,0 @@ -# $schema: ../schema.json -name: $elemMatch -link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/' -type: - - projection -encode: single -description: | - Projects the first element in an array that matches the specified $elemMatch condition. -arguments: - - - name: query - type: - - query -tests: - - - name: 'Zipcode Search' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#zipcode-search' - pipeline: - - - $match: - zipcode: '63109' - - - $project: - students: - $elemMatch: - school: 102 - - - name: 'with Multiple Fields' - link: 'https://www.mongodb.com/docs/manual/reference/operator/projection/elemMatch/#with-multiple-fields' - pipeline: - - - $match: - zipcode: '63109' - - - $project: - students: - $elemMatch: - school: 102 - age: - $gt: 10 diff --git a/generator/config/query/natural.yaml b/generator/config/query/natural.yaml deleted file mode 100644 index 0d81e90..0000000 --- a/generator/config/query/natural.yaml +++ /dev/null @@ -1,8 +0,0 @@ -# $schema: ../schema.json -name: $natural -link: 'https://www.mongodb.com/docs/manual/reference/operator/meta/natural/' -type: - - projection # @todo: used in sort -encode: object -description: | - A special hint that can be provided via the sort() or hint() methods that can be used to force either a forward or reverse collection scan. diff --git a/generator/config/schema.json b/generator/config/schema.json index 11ccc1e..b48b65a 100644 --- a/generator/config/schema.json +++ b/generator/config/schema.json @@ -23,7 +23,6 @@ "type": "string", "enum": [ "accumulator", - "projection", "stage", "query", "fieldQuery", @@ -113,7 +112,6 @@ "window", "expression", "geometry", - "projection", "fieldPath", "any", "resolvesToNumber", "numberFieldPath", "number", diff --git a/generator/config/stage/project.yaml b/generator/config/stage/project.yaml index ee45a0f..2337940 100644 --- a/generator/config/stage/project.yaml +++ b/generator/config/stage/project.yaml @@ -11,5 +11,4 @@ arguments: name: specification type: - expression - - projection variadic: object diff --git a/src/Builder/Expression/SliceOperator.php b/src/Builder/Expression/SliceOperator.php index 2f4a709..df91721 100644 --- a/src/Builder/Expression/SliceOperator.php +++ b/src/Builder/Expression/SliceOperator.php @@ -12,7 +12,6 @@ use MongoDB\Builder\Type\Encode; use MongoDB\Builder\Type\OperatorInterface; use MongoDB\Builder\Type\Optional; -use MongoDB\Builder\Type\ProjectionInterface; use MongoDB\Exception\InvalidArgumentException; use MongoDB\Model\BSONArray; @@ -24,7 +23,7 @@ * * @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/slice/ */ -class SliceOperator implements ResolvesToArray, ProjectionInterface, OperatorInterface +class SliceOperator implements ResolvesToArray, OperatorInterface { public const ENCODE = Encode::Array; diff --git a/src/Builder/Query/FactoryTrait.php b/src/Builder/Query/FactoryTrait.php index cc480de..fc8cb80 100644 --- a/src/Builder/Query/FactoryTrait.php +++ b/src/Builder/Query/FactoryTrait.php @@ -339,16 +339,6 @@ public static function mod( return new ModOperator($divisor, $remainder); } - /** - * A special hint that can be provided via the sort() or hint() methods that can be used to force either a forward or reverse collection scan. - * - * @see https://www.mongodb.com/docs/manual/reference/operator/meta/natural/ - */ - public static function natural(): NaturalOperator - { - return new NaturalOperator(); - } - /** * Matches all values that are not equal to a specified value. * diff --git a/src/Builder/Query/NaturalOperator.php b/src/Builder/Query/NaturalOperator.php deleted file mode 100644 index 4099209..0000000 --- a/src/Builder/Query/NaturalOperator.php +++ /dev/null @@ -1,32 +0,0 @@ - $specification */ + /** @var stdClass $specification */ public readonly stdClass $specification; /** - * @param Document|ExpressionInterface|ProjectionInterface|Serializable|Type|array|bool|float|int|null|stdClass|string ...$specification + * @param ExpressionInterface|Type|array|bool|float|int|null|stdClass|string ...$specification */ - public function __construct( - Document|Serializable|Type|ExpressionInterface|ProjectionInterface|stdClass|array|bool|float|int|null|string ...$specification, - ) { + public function __construct(Type|ExpressionInterface|stdClass|array|bool|float|int|null|string ...$specification) + { if (\count($specification) < 1) { throw new \InvalidArgumentException(\sprintf('Expected at least %d values for $specification, got %d.', 1, \count($specification))); } diff --git a/tests/Builder/Projection/ElemMatchOperatorTest.php b/tests/Builder/Projection/ElemMatchOperatorTest.php deleted file mode 100644 index a8e75f8..0000000 --- a/tests/Builder/Projection/ElemMatchOperatorTest.php +++ /dev/null @@ -1,54 +0,0 @@ -assertSamePipeline(Pipelines::ElemMatchWithMultipleFields, $pipeline); - } - - public function testZipcodeSearch(): void - { - $pipeline = new Pipeline( - Stage::match( - zipcode: '63109', - ), - Stage::project( - students: Projection::elemMatch( - Query::query( - school: 102, - ), - ), - ), - ); - - $this->assertSamePipeline(Pipelines::ElemMatchZipcodeSearch, $pipeline); - } -} diff --git a/tests/Builder/Projection/Pipelines.php b/tests/Builder/Projection/Pipelines.php deleted file mode 100644 index e80cb34..0000000 --- a/tests/Builder/Projection/Pipelines.php +++ /dev/null @@ -1,69 +0,0 @@ -