Skip to content

Commit a939aa0

Browse files
authored
Add first, firstN, last, lastN operators and tests (mongodb#14)
* Add first,firstN,last,lastN operators and tests * Add tests on $first and $last
1 parent 0bedc20 commit a939aa0

32 files changed

+1379
-64
lines changed

generator/config/accumulator/first.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ arguments:
1313
name: expression
1414
type:
1515
- expression
16+
tests:
17+
-
18+
name: 'Use in $group Stage'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/#use-in--group-stage'
20+
pipeline:
21+
-
22+
$sort:
23+
item: 1
24+
date: 1
25+
-
26+
$group:
27+
_id: '$item'
28+
firstSale:
29+
$first: '$date'
30+
-
31+
name: 'Use in $setWindowFields Stage'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/#use-in--setwindowfields-stage'
33+
pipeline:
34+
-
35+
$setWindowFields:
36+
partitionBy: '$state'
37+
sortBy:
38+
orderDate: 1
39+
output:
40+
firstOrderTypeForState:
41+
$first: '$type'
42+
window:
43+
documents:
44+
- 'unbounded'
45+
- 'current'

generator/config/accumulator/firstN.yaml

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,52 @@ type:
66
- window
77
encode: object
88
description: |
9-
Returns a specified number of elements from the beginning of an array. Distinct from the $firstN accumulator.
9+
Returns an aggregation of the first n elements within a group.
10+
The elements returned are meaningful only if in a specified sort order.
11+
If the group contains fewer than n elements, $firstN returns all elements in the group.
1012
arguments:
1113
-
1214
name: input
1315
type:
14-
- resolvesToArray
16+
- expression
1517
description: |
1618
An expression that resolves to the array from which to return n elements.
1719
-
1820
name: 'n'
1921
type:
2022
- resolvesToInt
2123
description: |
22-
An expression that resolves to a positive integer. The integer specifies the number of array elements that $firstN returns.
24+
A positive integral expression that is either a constant or depends on the _id value for $group.
25+
tests:
26+
-
27+
name: 'Null and Missing Values'
28+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#null-and-missing-values'
29+
pipeline:
30+
-
31+
$documents:
32+
-
33+
playerId: 'PlayerA'
34+
gameId: 'G1'
35+
score: 1
36+
-
37+
playerId: 'PlayerB'
38+
gameId: 'G1'
39+
score: 2
40+
-
41+
playerId: 'PlayerC'
42+
gameId: 'G1'
43+
score: 3
44+
-
45+
playerId: 'PlayerD'
46+
gameId: 'G1'
47+
-
48+
playerId: 'PlayerE'
49+
gameId: 'G1'
50+
score: ~
51+
-
52+
$group:
53+
_id: '$gameId'
54+
firstFiveScores:
55+
$firstN:
56+
input: '$score'
57+
n: 5

generator/config/accumulator/last.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,33 @@ arguments:
1313
name: expression
1414
type:
1515
- expression
16+
tests:
17+
-
18+
name: 'Use in $group Stage'
19+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/last/'
20+
pipeline:
21+
-
22+
$sort:
23+
item: 1
24+
date: 1
25+
-
26+
$group:
27+
_id: '$item'
28+
lastSalesDate:
29+
$last: '$date'
30+
-
31+
name: 'Use in $setWindowFields Stage'
32+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/last/#use-in--setwindowfields-stage'
33+
pipeline:
34+
-
35+
$setWindowFields:
36+
partitionBy: '$state'
37+
sortBy:
38+
orderDate: 1
39+
output:
40+
lastOrderTypeForState:
41+
$last: '$type'
42+
window:
43+
documents:
44+
- 'current'
45+
- 'unbounded'

generator/config/accumulator/lastN.yaml

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
name: $lastN
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/'
44
type:
5+
- accumulator
56
- window
67
encode: object
78
description: |
8-
Returns a specified number of elements from the end of an array. Distinct from the $lastN accumulator.
9+
Returns an aggregation of the last n elements within a group.
10+
The elements returned are meaningful only if in a specified sort order.
11+
If the group contains fewer than n elements, $lastN returns all elements in the group.
912
arguments:
1013
-
1114
name: input
@@ -19,3 +22,68 @@ arguments:
1922
- resolvesToInt
2023
description: |
2124
An expression that resolves to a positive integer. The integer specifies the number of array elements that $firstN returns.
25+
tests:
26+
-
27+
name: 'Find the Last Three Player Scores for a Single Game'
28+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#find-the-last-three-player-scores-for-a-single-game'
29+
pipeline:
30+
-
31+
$match:
32+
gameId: 'G1'
33+
-
34+
$group:
35+
_id: '$gameId'
36+
lastThreeScores:
37+
$lastN:
38+
input:
39+
- '$playerId'
40+
- '$score'
41+
n: 3
42+
-
43+
name: 'Finding the Last Three Player Scores Across Multiple Games'
44+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#finding-the-last-three-player-scores-across-multiple-games'
45+
pipeline:
46+
-
47+
$group:
48+
_id: '$gameId'
49+
playerId:
50+
$lastN:
51+
input:
52+
- '$playerId'
53+
- '$score'
54+
n: 3
55+
-
56+
name: 'Using $sort With $lastN'
57+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#using--sort-with--lastn'
58+
pipeline:
59+
-
60+
$sort:
61+
score: -1
62+
-
63+
$group:
64+
_id: '$gameId'
65+
playerId:
66+
$lastN:
67+
input:
68+
- '$playerId'
69+
- '$score'
70+
n: 3
71+
-
72+
name: 'Computing n Based on the Group Key for $group'
73+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#computing-n-based-on-the-group-key-for--group'
74+
pipeline:
75+
-
76+
$group:
77+
_id:
78+
gameId: '$gameId'
79+
gamescores:
80+
$lastN:
81+
input: '$score'
82+
n:
83+
$cond:
84+
if:
85+
$eq:
86+
- '$gameId'
87+
- 'G2'
88+
then: 1
89+
else: 3
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# $schema: ../schema.json
2+
name: $first
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/first/'
4+
type:
5+
- resolvesToAny
6+
encode: single
7+
description: |
8+
Returns the result of an expression for the first document in an array.
9+
arguments:
10+
-
11+
name: expression
12+
type:
13+
- resolvesToArray
14+
tests:
15+
-
16+
name: 'Use in $addFields Stage'
17+
pipeline:
18+
-
19+
$addFields:
20+
firstItem:
21+
$first: '$items'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# $schema: ../schema.json
2+
name: $firstN
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN-array-element/'
4+
type:
5+
- resolvesToArray
6+
encode: object
7+
description: |
8+
Returns a specified number of elements from the beginning of an array.
9+
arguments:
10+
-
11+
name: 'n'
12+
type:
13+
- resolvesToInt
14+
description: |
15+
An expression that resolves to a positive integer. The integer specifies the number of array elements that $firstN returns.
16+
-
17+
name: input
18+
type:
19+
- resolvesToArray
20+
description: |
21+
An expression that resolves to the array from which to return n elements.
22+
23+
tests:
24+
-
25+
name: Example
26+
link: https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN-array-element/#example
27+
pipeline:
28+
-
29+
$addFields:
30+
firstScores:
31+
$firstN:
32+
n: 3
33+
input: '$score'

generator/config/expression/last.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# $schema: ../schema.json
2+
name: $last
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/last/'
4+
type:
5+
- resolvesToAny
6+
encode: single
7+
description: |
8+
Returns the result of an expression for the last document in an array.
9+
arguments:
10+
-
11+
name: expression
12+
type:
13+
- resolvesToArray
14+
tests:
15+
-
16+
name: 'Use in $addFields Stage'
17+
pipeline:
18+
-
19+
$addFields:
20+
lastItem:
21+
$last: '$items'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# $schema: ../schema.json
2+
name: $lastN
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN-array-element/'
4+
type:
5+
- resolvesToArray
6+
encode: object
7+
description: |
8+
Returns a specified number of elements from the end of an array.
9+
arguments:
10+
-
11+
name: 'n'
12+
type:
13+
- resolvesToInt
14+
description: |
15+
An expression that resolves to a positive integer. The integer specifies the number of array elements that $firstN returns.
16+
-
17+
name: input
18+
type:
19+
- resolvesToArray
20+
description: |
21+
An expression that resolves to the array from which to return n elements.
22+
tests:
23+
-
24+
name: Example
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN-array-element/#example'
26+
pipeline:
27+
-
28+
$addFields:
29+
lastScores:
30+
$lastN:
31+
n: 3
32+
input: '$score'
33+
34+
-
35+
name: 'Using $lastN as an Aggregation Expression'
36+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/lastN/#using--lastn-as-an-aggregation-expression'
37+
pipeline:
38+
-
39+
$documents:
40+
-
41+
array:
42+
- 10
43+
- 20
44+
- 30
45+
- 40
46+
-
47+
$project:
48+
lastThreeElements:
49+
$lastN:
50+
input: '$array'
51+
n: 3

generator/config/stage/project.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ arguments:
1010
-
1111
name: specification
1212
type:
13-
- bool
14-
- int
13+
- expression
1514
- projection
16-
- resolvesToBool
17-
- object
1815
variadic: object

generator/config/stage/setWindowFields.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,3 @@ arguments:
2727
description: |
2828
Specifies the field(s) to append to the documents in the output returned by the $setWindowFields stage. Each field is set to the result returned by the window operator.
2929
A field can contain dots to specify embedded document fields and array fields. The semantics for the embedded document dotted notation in the $setWindowFields stage are the same as the $addFields and $set stages.
30-
-
31-
name: window
32-
type:
33-
- window
34-
optional: true
35-
description: |
36-
Specifies the window boundaries and parameters. Window boundaries are inclusive. Default is an unbounded window, which includes all documents in the partition.

src/Builder/Accumulator/FactoryTrait.php

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

0 commit comments

Comments
 (0)