Skip to content

Commit 5e8b98a

Browse files
authored
PHPLIB-1343 Add tests on Array Expression Operators (mongodb#22)
1 parent 4176b5a commit 5e8b98a

39 files changed

+2434
-51
lines changed

generator/config/expression/arrayElemAt.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,19 @@ arguments:
1515
name: idx
1616
type:
1717
- resolvesToInt
18+
tests:
19+
-
20+
name: 'Example'
21+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayElemAt/#example'
22+
pipeline:
23+
-
24+
$project:
25+
name: 1
26+
first:
27+
$arrayElemAt:
28+
- '$favorites'
29+
- 0
30+
last:
31+
$arrayElemAt:
32+
- '$favorites'
33+
- -1

generator/config/expression/arrayToObject.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,40 @@ arguments:
1111
name: array
1212
type:
1313
- resolvesToArray
14+
tests:
15+
-
16+
name: '$arrayToObject Example'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayToObject/#-arraytoobject--example'
18+
pipeline:
19+
-
20+
$project:
21+
item: 1
22+
dimensions:
23+
# The example renders a single value, but the builder generates an array for consistency
24+
# $arrayToObject: '$dimensions'
25+
$arrayToObject:
26+
- '$dimensions'
27+
-
28+
name: '$objectToArray and $arrayToObject Example'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/arrayToObject/#-objecttoarray----arraytoobject-example'
30+
pipeline:
31+
-
32+
$addFields:
33+
instock:
34+
$objectToArray: '$instock'
35+
-
36+
$addFields:
37+
instock:
38+
$concatArrays:
39+
- '$instock'
40+
-
41+
-
42+
k: 'total'
43+
v:
44+
$sum:
45+
- '$instock.v'
46+
-
47+
$addFields:
48+
instock:
49+
$arrayToObject:
50+
- '$instock'

generator/config/expression/concatArrays.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ arguments:
1212
type:
1313
- resolvesToArray
1414
variadic: array
15+
tests:
16+
-
17+
name: 'Example'
18+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/concatArrays/#example'
19+
pipeline:
20+
-
21+
$project:
22+
items:
23+
$concatArrays:
24+
- '$instock'
25+
- '$ordered'

generator/config/expression/filter.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,63 @@ arguments:
3232
description: |
3333
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.
3434
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.
35+
tests:
36+
-
37+
name: 'Example'
38+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#examples'
39+
pipeline:
40+
-
41+
$project:
42+
items:
43+
$filter:
44+
input: '$items'
45+
as: 'item'
46+
cond:
47+
$gte:
48+
- '$$item.price'
49+
- 100
50+
-
51+
name: 'Using the limit field'
52+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#using-the-limit-field'
53+
pipeline:
54+
-
55+
$project:
56+
items:
57+
$filter:
58+
input: '$items'
59+
cond:
60+
$gte:
61+
- '$$item.price'
62+
- 100
63+
as: 'item'
64+
limit: 1
65+
-
66+
name: 'limit as a Numeric Expression'
67+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#limit-as-a-numeric-expression'
68+
pipeline:
69+
-
70+
$project:
71+
items:
72+
$filter:
73+
input: '$items'
74+
cond:
75+
$lte:
76+
- '$$item.price'
77+
- 150
78+
as: 'item'
79+
limit: 2
80+
-
81+
name: 'limit Greater than Possible Matches'
82+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/#limit-greater-than-possible-matches'
83+
pipeline:
84+
-
85+
$project:
86+
items:
87+
$filter:
88+
input: '$items'
89+
cond:
90+
$gte:
91+
- '$$item.price'
92+
- 100
93+
as: 'item'
94+
limit: 5

generator/config/expression/in.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ arguments:
1919
- resolvesToArray
2020
description: |
2121
Any valid expression that resolves to an array.
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/in/#example'
26+
pipeline:
27+
-
28+
$project:
29+
store location: '$location'
30+
has bananas:
31+
$in:
32+
- 'bananas'
33+
- '$in_stock'

generator/config/expression/indexOfArray.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ arguments:
3535
description: |
3636
An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfArray uses the <end> value as the <start> index value instead of the <end> value.
3737
If unspecified, the ending index position for the search is the end of the string.
38+
tests:
39+
-
40+
name: 'Example'
41+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexOfArray/#example'
42+
pipeline:
43+
-
44+
$project:
45+
index:
46+
$indexOfArray:
47+
- '$items'
48+
- 2

generator/config/expression/isArray.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,27 @@ arguments:
1111
name: expression
1212
type:
1313
- expression
14-
variadic: array
14+
tests:
15+
-
16+
name: 'Example'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/isArray/#example'
18+
pipeline:
19+
-
20+
$project:
21+
items:
22+
$cond:
23+
if:
24+
$and:
25+
# The example in the docs uses the short syntax for $isArray,
26+
# but the aggregation builder always uses the more verbose syntax.
27+
-
28+
$isArray:
29+
- '$instock'
30+
-
31+
$isArray:
32+
- '$ordered'
33+
then:
34+
$concatArrays:
35+
- '$instock'
36+
- '$ordered'
37+
else: 'One or more fields is not an array.'

generator/config/expression/map.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,51 @@ arguments:
2626
- expression
2727
description: |
2828
An expression that is applied to each element of the input array. The expression references each element individually with the variable name specified in as.
29+
tests:
30+
-
31+
name: 'Add to Each Element of an Array'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#add-to-each-element-of-an-array'
33+
pipeline:
34+
-
35+
$project:
36+
adjustedGrades:
37+
$map:
38+
input: '$quizzes'
39+
as: 'grade'
40+
in:
41+
$add:
42+
- '$$grade'
43+
- 2
44+
-
45+
name: 'Truncate Each Array Element'
46+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#truncate-each-array-element'
47+
pipeline:
48+
-
49+
$project:
50+
city: '$city'
51+
integerValues:
52+
$map:
53+
input: '$distances'
54+
as: 'decimalValue'
55+
in:
56+
# The example renders a single value, but the builder generates an array for consistency
57+
# $trunc: '$$decimalValue'
58+
$trunc:
59+
- '$$decimalValue'
60+
-
61+
name: 'Convert Celsius Temperatures to Fahrenheit'
62+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/#convert-celsius-temperatures-to-fahrenheit'
63+
pipeline:
64+
-
65+
$addFields:
66+
tempsF:
67+
$map:
68+
input: '$tempsC'
69+
as: 'tempInCelsius'
70+
in:
71+
$add:
72+
-
73+
$multiply:
74+
- '$$tempInCelsius'
75+
- 1.8
76+
- 32

generator/config/expression/maxN.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ arguments:
1919
- resolvesToInt
2020
description: |
2121
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN-array-element/#example'
26+
pipeline:
27+
-
28+
$addFields:
29+
maxScores:
30+
$maxN:
31+
n: 2
32+
input: '$score'

generator/config/expression/minN.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,14 @@ arguments:
1919
- resolvesToInt
2020
description: |
2121
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
22+
tests:
23+
-
24+
name: 'Example'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN-array-element/#example'
26+
pipeline:
27+
-
28+
$addFields:
29+
minScores:
30+
$minN:
31+
n: 2
32+
input: '$score'

generator/config/expression/objectToArray.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ arguments:
1313
- resolvesToObject
1414
description: |
1515
Any valid expression as long as it resolves to a document object. $objectToArray applies to the top-level fields of its argument. If the argument is a document that itself contains embedded document fields, the $objectToArray does not recursively apply to the embedded document fields.
16+
tests:
17+
# "$objectToArray and $arrayToObject Example" omitted as it's already in arrayToObject.yaml
18+
-
19+
name: '$objectToArray Example'
20+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/#-objecttoarray-example'
21+
pipeline:
22+
-
23+
$project:
24+
item: 1
25+
dimensions:
26+
$objectToArray: '$dimensions'
27+
-
28+
name: '$objectToArray to Sum Nested Fields'
29+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/objectToArray/#-objecttoarray-to-sum-nested-fields'
30+
pipeline:
31+
-
32+
$project:
33+
warehouses:
34+
$objectToArray: '$instock'
35+
-
36+
# The example in the docs uses the short syntax for $unwind,
37+
# but the aggregation builder always uses the more verbose syntax.
38+
# $unwind: '$warehouses'
39+
$unwind:
40+
path: '$warehouses'
41+
-
42+
$group:
43+
_id: '$warehouses.k'
44+
total:
45+
$sum: '$warehouses.v'

generator/config/expression/range.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ arguments:
2626
optional: true
2727
description: |
2828
An integer that specifies the increment value. Can be any valid expression that resolves to a non-zero integer. Defaults to 1.
29+
tests:
30+
-
31+
name: 'Example'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/range/#example'
33+
pipeline:
34+
-
35+
$project:
36+
_id: 0
37+
city: 1
38+
Rest stops:
39+
$range:
40+
- 0
41+
- '$distance'
42+
- 25

0 commit comments

Comments
 (0)