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

Various improvements #19

Merged
merged 7 commits into from
Dec 22, 2023
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: 28 additions & 2 deletions generator/config/expression/add.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
name: $add
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/'
type:
- resolvesToNumber
- resolvesToInt
- resolvesToLong
- resolvesToDouble
- resolvesToDecimal
- resolvesToDate
encode: array
encode: single
description: |
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.
arguments:
Expand All @@ -16,3 +19,26 @@ arguments:
variadic: array
description: |
The arguments can be any valid expression as long as they resolve to either all numbers or to numbers and a date.
tests:
-
name: 'Add Numbers'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/#add-numbers'
pipeline:
-
$project:
item: 1
total:
$add:
- '$price'
- '$fee'
-
name: 'Perform Addition on a Date'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/add/#perform-addition-on-a-date'
pipeline:
-
$project:
item: 1
billing_date:
$add:
- '$date'
- 259200000
39 changes: 39 additions & 0 deletions generator/config/expression/mergeObjects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# $schema: ../schema.json
name: $mergeObjects
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/'
type:
- resolvesToObject
encode: single
description: |
Combines multiple documents into a single document.
arguments:
-
name: document
type:
- resolvesToObject
variadic: array
description: |
Any valid expression that resolves to a document.
tests:
-
name: '$mergeObjects'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/mergeObjects/#-mergeobjects'
pipeline:
-
$lookup:
from: 'items'
localField: 'item'
foreignField: 'item'
as: 'fromItems'
-
$replaceRoot:
newRoot:
$mergeObjects:
-
$arrayElemAt:
- '$fromItems'
- 0
- '$$ROOT'
-
$project:
fromItems: 0
42 changes: 41 additions & 1 deletion generator/config/expression/subtract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
name: $subtract
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/'
type:
- resolvesToNumber
- resolvesToInt
- resolvesToLong
- resolvesToDouble
- resolvesToDecimal
- resolvesToDate
encode: array
description: |
Expand All @@ -18,3 +21,40 @@ arguments:
type:
- resolvesToNumber
- resolvesToDate
tests:
-
name: 'Subtract Numbers'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-numbers'
pipeline:
-
$project:
item: 1
total:
$subtract:
-
$add:
- '$price'
- '$fee'
- '$discount'
-
name: 'Subtract Two Dates'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-two-dates'
pipeline:
-
$project:
item: 1
dateDifference:
$subtract:
- '$$NOW'
- '$date'
-
name: 'Subtract Milliseconds from a Date'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/subtract/#subtract-milliseconds-from-a-date'
pipeline:
-
$project:
item: 1
dateDifference:
$subtract:
- '$date'
- 300000
55 changes: 55 additions & 0 deletions generator/config/expression/zip.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,69 @@ arguments:
name: useLongestLength
type:
- bool
optional: true
description: |
A boolean which specifies whether the length of the longest array determines the number of arrays in the output array.
The default value is false: the shortest array length determines the number of arrays in the output array.
-
name: defaults
type:
- array
optional: true
description: |
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.
If useLongestLength: true but defaults is empty or not specified, $zip uses null as the default value.
If specifying a non-empty defaults, you must specify a default for each input array or else $zip will return an error.
tests:
-
name: 'Matrix Transposition'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/zip/#matrix-transposition'
pipeline:
-
$project:
_id: false
transposed:
$zip:
inputs:
-
$arrayElemAt:
- '$matrix'
- 0
-
$arrayElemAt:
- '$matrix'
- 1
-
$arrayElemAt:
- '$matrix'
- 2
-
name: 'Filtering and Preserving Indexes'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/zip/#filtering-and-preserving-indexes'
pipeline:
-
$project:
_id: false
pages:
$filter:
input:
$zip:
inputs:
- '$pages'
-
$range:
- 0
-
$size: '$pages'
as: 'pageWithIndex'
cond:
$let:
vars:
page:
$arrayElemAt:
- '$$pageWithIndex'
- 0
in:
$gte:
- '$$page.reviews'
- 1
36 changes: 35 additions & 1 deletion generator/config/query/expr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: $expr
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/'
type:
- fieldQuery
- query
encode: single
description: |
Allows use of aggregation expressions within the query language.
Expand All @@ -11,3 +11,37 @@ arguments:
name: expression
type:
- expression
tests:
-
name: 'Compare Two Fields from A Single Document'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/#compare-two-fields-from-a-single-document'
pipeline:
-
$match:
$expr:
$gt:
- '$spent'
- '$budget'
-
name: 'Using $expr With Conditional Statements'
link: 'https://www.mongodb.com/docs/manual/reference/operator/query/expr/#using--expr-with-conditional-statements'
pipeline:
-
$match:
$expr:
$lt:
-
$cond:
if:
$gte:
- '$qty'
- 100
then:
$multiply:
- '$price'
- 0.5
else:
$multiply:
- '$price'
- 0.75
- 5
4 changes: 2 additions & 2 deletions src/Builder/Expression/AddOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions src/Builder/Expression/FactoryTrait.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/Builder/Expression/MergeObjectsOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Builder/Expression/SubtractOperator.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading