Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

PHPLIB-1359 Add tests on Accumulators ($group, $bucket, $bucketAuto, $setWindowFields) #29

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions generator/config/accumulator/avg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,33 @@ arguments:
name: expression
type:
- resolvesToNumber
tests:
-
name: 'Use in $group Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--group-stage'
pipeline:
- $group:
_id: '$item'
avgAmount:
$avg:
$multiply:
- '$price'
- '$quantity'
avgQuantity:
$avg: '$quantity'
-
name: 'Use in $setWindowFields Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#use-in--setwindowfields-stage'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
averageQuantityForState:
$avg: '$quantity'
window:
documents:
- 'unbounded'
- 'current'
32 changes: 32 additions & 0 deletions generator/config/accumulator/bottom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,35 @@ arguments:
- expression
description: |
Represents the output for each element in the group and can be any expression.
tests:
-
name: 'Find the Bottom Score'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottom/#find-the-bottom-score'
pipeline:
-
$match:
gameId: 'G1'
-
$group:
_id: '$gameId'
playerId:
$bottom:
output:
- '$playerId'
- '$score'
sortBy:
score: -1
-
name: 'Finding the Bottom Score Across Multiple Games'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottom/#finding-the-bottom-score-across-multiple-games'
pipeline:
-
$group:
_id: '$gameId'
playerId:
$bottom:
output:
- '$playerId'
- '$score'
sortBy:
score: -1
55 changes: 55 additions & 0 deletions generator/config/accumulator/bottomN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,58 @@ arguments:
- expression
description: |
Represents the output for each element in the group and can be any expression.
tests:
-
name: 'Find the Three Lowest Scores'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#find-the-three-lowest-scores'
pipeline:
-
$match:
gameId: 'G1'
-
$group:
_id: '$gameId'
playerId:
$bottomN:
output:
- '$playerId'
- '$score'
sortBy:
score: -1
n: 3
-
name: 'Finding the Three Lowest Score Documents Across Multiple Games'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#finding-the-three-lowest-score-documents-across-multiple-games'
pipeline:
-
$group:
_id: '$gameId'
playerId:
$bottomN:
output:
- '$playerId'
- '$score'
sortBy:
score: -1
n: 3
-
name: 'Computing n Based on the Group Key for $group'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/bottomN/#computing-n-based-on-the-group-key-for--group'
pipeline:
-
$group:
_id:
gameId: '$gameId'
gamescores:
$bottomN:
output: '$score'
n:
$cond:
if:
$eq:
- '$gameId'
- 'G2'
then: 1
else: 3
sortBy:
score: -1
26 changes: 26 additions & 0 deletions generator/config/accumulator/count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,29 @@ description: |
Returns the number of documents in the group or window.
Distinct from the $count pipeline stage.
New in MongoDB 5.0.
tests:
-
name: 'Use in $group Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--group-stage'
pipeline:
-
$group:
_id: '$state'
countNumberOfDocumentsForState:
$count: {}
-
name: 'Use in $setWindowFields Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator/#use-in--setwindowfields-stage'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
countNumberOfDocumentsForState:
$count: {}
window:
documents:
- 'unbounded'
- 'current'
65 changes: 65 additions & 0 deletions generator/config/accumulator/firstN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,68 @@ tests:
$firstN:
input: '$score'
n: 5
-
name: 'Find the First Three Player Scores for a Single Game'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#find-the-first-three-player-scores-for-a-single-game'
pipeline:
-
$match:
gameId: 'G1'
-
$group:
_id: '$gameId'
firstThreeScores:
$firstN:
input:
- '$playerId'
- '$score'
n: 3
-
name: 'Finding the First Three Player Scores Across Multiple Games'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#finding-the-first-three-player-scores-across-multiple-games'
pipeline:
-
$group:
_id: '$gameId'
playerId:
$firstN:
input:
- '$playerId'
- '$score'
n: 3
-
name: 'Using $sort With $firstN'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#using--sort-with--firstn'
pipeline:
-
$sort:
score: -1
-
$group:
_id: '$gameId'
playerId:
$firstN:
input:
- '$playerId'
- '$score'
n: 3
-
name: 'Computing n Based on the Group Key for $group'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/firstN/#computing-n-based-on-the-group-key-for--group'
pipeline:
-
$group:
_id:
gameId: '$gameId'
gamescores:
$firstN:
input: '$score'
n:
$cond:
if:
$eq:
- '$gameId'
- 'G2'
then: 1
else: 3

31 changes: 31 additions & 0 deletions generator/config/accumulator/max.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,34 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Use in $group Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#use-in--group-stage'
pipeline:
-
$group:
_id: '$item'
maxTotalAmount:
$max:
$multiply:
- '$price'
- '$quantity'
maxQuantity:
$max: '$quantity'
-
name: 'Use in $setWindowFields Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/max/#use-in--setwindowfields-stage'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
maximumQuantityForState:
$max: '$quantity'
window:
documents:
- 'unbounded'
- 'current'
51 changes: 51 additions & 0 deletions generator/config/accumulator/maxN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,54 @@ arguments:
- resolvesToInt
description: |
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
tests:
-
name: 'Find the Maximum Three Scores for a Single Game'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#find-the-maximum-three-scores-for-a-single-game'
pipeline:
-
$match:
gameId: 'G1'
-
$group:
_id: '$gameId'
maxThreeScores:
$maxN:
input:
- '$score'
- '$playerId'
n: 3
-
name: 'Finding the Maximum Three Scores Across Multiple Games'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#finding-the-maximum-three-scores-across-multiple-games'
pipeline:
-
$group:
_id: '$gameId'
maxScores:
$maxN:
input:
- '$score'
- '$playerId'
n: 3
-
name: 'Computing n Based on the Group Key for $group'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/maxN/#computing-n-based-on-the-group-key-for--group'
pipeline:
-
$group:
_id:
gameId: '$gameId'
gamescores:
$maxN:
input:
- '$score'
- '$playerId'
n:
$cond:
if:
$eq:
- '$gameId'
- 'G2'
then: 1
else: 3
34 changes: 34 additions & 0 deletions generator/config/accumulator/median.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,37 @@ arguments:
- string # AccumulatorPercentile
description: |
The method that mongod uses to calculate the 50th percentile value. The method must be 'approximate'.
tests:
-
name: 'Use $median as an Accumulator'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/median/#use-operatorname-as-an-accumulator'
pipeline:
-
$group:
_id: ~
test01_median:
$median:
input: '$test01'
method: 'approximate'
-
name: 'Use $median in a $setWindowField Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/median/#use-operatorname-in-a--setwindowfield-stage'
pipeline:
-
$setWindowFields:
sortBy:
test01: 1
output:
test01_median:
$median:
input: '$test01'
method: 'approximate'
window:
range:
- -3
- 3
-
$project:
_id: 0
studentId: 1
test01_median: 1
11 changes: 10 additions & 1 deletion generator/config/accumulator/mergeObjects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ arguments:
name: document
type:
- resolvesToObject
variadic: array
description: |
Any valid expression that resolves to a document.
tests:
-
name: '$mergeObjects as an Accumulator'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/#-mergeobjects-as-an-accumulator'
pipeline:
-
$group:
_id: '$item'
mergedSales:
$mergeObjects: '$quantity'
Loading