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

Commit b0886f8

Browse files
authored
Various improvements (#19)
1 parent 48353b4 commit b0886f8

18 files changed

+887
-21
lines changed

generator/config/expression/add.yaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
name: $add
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/'
44
type:
5-
- resolvesToNumber
5+
- resolvesToInt
6+
- resolvesToLong
7+
- resolvesToDouble
8+
- resolvesToDecimal
69
- resolvesToDate
7-
encode: array
10+
encode: single
811
description: |
912
Adds numbers to return the sum, or adds numbers and a date to return a new date. If adding numbers and a date, treats the numbers as milliseconds. Accepts any number of argument expressions, but at most, one expression can resolve to a date.
1013
arguments:
@@ -16,3 +19,26 @@ arguments:
1619
variadic: array
1720
description: |
1821
The arguments can be any valid expression as long as they resolve to either all numbers or to numbers and a date.
22+
tests:
23+
-
24+
name: 'Add Numbers'
25+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/#add-numbers'
26+
pipeline:
27+
-
28+
$project:
29+
item: 1
30+
total:
31+
$add:
32+
- '$price'
33+
- '$fee'
34+
-
35+
name: 'Perform Addition on a Date'
36+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/#perform-addition-on-a-date'
37+
pipeline:
38+
-
39+
$project:
40+
item: 1
41+
billing_date:
42+
$add:
43+
- '$date'
44+
- 259200000
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# $schema: ../schema.json
2+
name: $mergeObjects
3+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/'
4+
type:
5+
- resolvesToObject
6+
encode: single
7+
description: |
8+
Combines multiple documents into a single document.
9+
arguments:
10+
-
11+
name: document
12+
type:
13+
- resolvesToObject
14+
variadic: array
15+
description: |
16+
Any valid expression that resolves to a document.
17+
tests:
18+
-
19+
name: '$mergeObjects'
20+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/#-mergeobjects'
21+
pipeline:
22+
-
23+
$lookup:
24+
from: 'items'
25+
localField: 'item'
26+
foreignField: 'item'
27+
as: 'fromItems'
28+
-
29+
$replaceRoot:
30+
newRoot:
31+
$mergeObjects:
32+
-
33+
$arrayElemAt:
34+
- '$fromItems'
35+
- 0
36+
- '$$ROOT'
37+
-
38+
$project:
39+
fromItems: 0

generator/config/expression/subtract.yaml

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
name: $subtract
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/'
44
type:
5-
- resolvesToNumber
5+
- resolvesToInt
6+
- resolvesToLong
7+
- resolvesToDouble
8+
- resolvesToDecimal
69
- resolvesToDate
710
encode: array
811
description: |
@@ -18,3 +21,40 @@ arguments:
1821
type:
1922
- resolvesToNumber
2023
- resolvesToDate
24+
tests:
25+
-
26+
name: 'Subtract Numbers'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-numbers'
28+
pipeline:
29+
-
30+
$project:
31+
item: 1
32+
total:
33+
$subtract:
34+
-
35+
$add:
36+
- '$price'
37+
- '$fee'
38+
- '$discount'
39+
-
40+
name: 'Subtract Two Dates'
41+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-two-dates'
42+
pipeline:
43+
-
44+
$project:
45+
item: 1
46+
dateDifference:
47+
$subtract:
48+
- '$$NOW'
49+
- '$date'
50+
-
51+
name: 'Subtract Milliseconds from a Date'
52+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-milliseconds-from-a-date'
53+
pipeline:
54+
-
55+
$project:
56+
item: 1
57+
dateDifference:
58+
$subtract:
59+
- '$date'
60+
- 300000

generator/config/expression/zip.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,69 @@ arguments:
1919
name: useLongestLength
2020
type:
2121
- bool
22+
optional: true
2223
description: |
2324
A boolean which specifies whether the length of the longest array determines the number of arrays in the output array.
2425
The default value is false: the shortest array length determines the number of arrays in the output array.
2526
-
2627
name: defaults
2728
type:
2829
- array
30+
optional: true
2931
description: |
3032
An array of default element values to use if the input arrays have different lengths. You must specify useLongestLength: true along with this field, or else $zip will return an error.
3133
If useLongestLength: true but defaults is empty or not specified, $zip uses null as the default value.
3234
If specifying a non-empty defaults, you must specify a default for each input array or else $zip will return an error.
35+
tests:
36+
-
37+
name: 'Matrix Transposition'
38+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/zip/#matrix-transposition'
39+
pipeline:
40+
-
41+
$project:
42+
_id: false
43+
transposed:
44+
$zip:
45+
inputs:
46+
-
47+
$arrayElemAt:
48+
- '$matrix'
49+
- 0
50+
-
51+
$arrayElemAt:
52+
- '$matrix'
53+
- 1
54+
-
55+
$arrayElemAt:
56+
- '$matrix'
57+
- 2
58+
-
59+
name: 'Filtering and Preserving Indexes'
60+
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/zip/#filtering-and-preserving-indexes'
61+
pipeline:
62+
-
63+
$project:
64+
_id: false
65+
pages:
66+
$filter:
67+
input:
68+
$zip:
69+
inputs:
70+
- '$pages'
71+
-
72+
$range:
73+
- 0
74+
-
75+
$size: '$pages'
76+
as: 'pageWithIndex'
77+
cond:
78+
$let:
79+
vars:
80+
page:
81+
$arrayElemAt:
82+
- '$$pageWithIndex'
83+
- 0
84+
in:
85+
$gte:
86+
- '$$page.reviews'
87+
- 1

generator/config/query/expr.yaml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: $expr
33
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/'
44
type:
5-
- fieldQuery
5+
- query
66
encode: single
77
description: |
88
Allows use of aggregation expressions within the query language.
@@ -11,3 +11,37 @@ arguments:
1111
name: expression
1212
type:
1313
- expression
14+
tests:
15+
-
16+
name: 'Compare Two Fields from A Single Document'
17+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/#compare-two-fields-from-a-single-document'
18+
pipeline:
19+
-
20+
$match:
21+
$expr:
22+
$gt:
23+
- '$spent'
24+
- '$budget'
25+
-
26+
name: 'Using $expr With Conditional Statements'
27+
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/#using--expr-with-conditional-statements'
28+
pipeline:
29+
-
30+
$match:
31+
$expr:
32+
$lt:
33+
-
34+
$cond:
35+
if:
36+
$gte:
37+
- '$qty'
38+
- 100
39+
then:
40+
$multiply:
41+
- '$price'
42+
- 0.5
43+
else:
44+
$multiply:
45+
- '$price'
46+
- 0.75
47+
- 5

src/Builder/Expression/AddOperator.php

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

src/Builder/Expression/FactoryTrait.php

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

src/Builder/Expression/MergeObjectsOperator.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.

src/Builder/Expression/SubtractOperator.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)