Skip to content

DOCSP-14577 Let variables in aggregation expressions #5535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
12 changes: 7 additions & 5 deletions source/includes/let-example-introduction.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 5.0

To specify variables that can be accessed in the command, use the
``let`` option.
To define variables that you can access elsewhere in the command, use
the |let-option| option.

.. note::

To filter results using a variable, you must access the variable
within the :query:`$expr` operator.
5 changes: 5 additions & 0 deletions source/includes/let-variables-aggregate-syntax-note.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. note::

To use a variable to filter results in a pipeline :pipeline:`$match`
stage, you must access the variable within the :query:`$expr`
operator.
8 changes: 8 additions & 0 deletions source/includes/let-variables-match-note.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To define variables that you can access elsewhere in the command, use
the |let-option| option.

.. note::

To filter results using a variable in a pipeline :pipeline:`$match`
stage, you must access the variable within the :query:`$expr`
operator.
4 changes: 4 additions & 0 deletions source/includes/let-variables-syntax-note.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. note::

To use a variable to filter results, you must access the variable
within the :query:`$expr` operator.
11 changes: 0 additions & 11 deletions source/includes/let-variables-syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ The document syntax is:
The variable is set to the value returned by the expression, and cannot
be changed afterwards.

Examples:

- ``{ targetTotal: 3000 }``, sets ``targetTotal`` to ``3000``.

- ``{ totalCost: { $add: [ 125, 16, 765 ] } }``, sets ``totalCost`` to
``906``.

- ``{ myRecordingTime: "$$NOW" }``, sets ``myRecordingTime`` to the
current date and time returned by the :variable:`NOW` system
variable.

To access the value of a variable in the command, use the double
dollar sign prefix (``$$``) together with your variable name in the form
``$$<variable_name>``. For example: ``$$targetTotal``.
10 changes: 7 additions & 3 deletions source/reference/command/aggregate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ arguments:

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`aggregate-let-example`.
.. include:: /includes/let-variables-aggregate-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`aggregate-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -566,8 +569,9 @@ Use Variables in ``let``

.. versionadded:: 5.0

To specify variables that can be accessed elsewhere in the command, use
the :ref:`let <aggregate-let-option>` option.
.. |let-option| replace:: :ref:`let <aggregate-let-option>`

.. include:: /includes/let-variables-match-note.rst

.. include:: /includes/let-variables-example.rst

Expand Down
12 changes: 9 additions & 3 deletions source/reference/command/delete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ Definition

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`delete-let-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
see :ref:`delete-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -435,9 +438,12 @@ To see the index used, run :dbcommand:`explain` on the operation:

.. _delete-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <delete-let-syntax>`.
.. |let-option| replace:: :ref:`let <delete-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-delete-flavors.rst

Expand Down
12 changes: 9 additions & 3 deletions source/reference/command/find.txt
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ The command accepts the following fields:

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`find-let-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`find-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -633,9 +636,12 @@ specify :ref:`collation <collation>` for a

.. _find-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <find-let-syntax>`.
.. |let-option| replace:: :ref:`let <find-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-find-flavors.rst

Expand Down
12 changes: 9 additions & 3 deletions source/reference/command/findAndModify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ Definition

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`findAndModify-let-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`findAndModify-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -1014,9 +1017,12 @@ To see the index used, run :dbcommand:`explain` on the operation:

.. _findAndModify-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <findAndModify-let-syntax>`.
.. |let-option| replace:: :ref:`let <findAndModify-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-find-modify-flavors.rst

Expand Down
12 changes: 10 additions & 2 deletions source/reference/command/update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The command takes the following fields:

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`update-variables-example`.
For a complete MQL example, see :ref:`update-variables-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -213,7 +213,10 @@ Each document contains the following fields:

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`update-variables-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`update-variables-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -1089,6 +1092,11 @@ Variables can be defined in the :ref:`let <update-let-syntax>` option or
the :ref:`c <update-command-c>` field and accessed in the ``updates``
array.

.. note::

To filter results using a variable, you must access the variable
within the :query:`$expr` operator.

.. include:: /includes/let-example-update-flavors.rst

.. code-block:: javascript
Expand Down
9 changes: 6 additions & 3 deletions source/reference/method/db.collection.aggregate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ Definition

.. include:: /includes/let-variables-syntax.rst

For an example, see
.. include:: /includes/let-variables-aggregate-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`db.collection.aggregate-let-example`.

.. versionadded:: 5.0
Expand Down Expand Up @@ -632,8 +634,9 @@ Use Variables in ``let``

.. versionadded:: 5.0

To specify variables that can be accessed elsewhere in the command, use
the :ref:`let <db.collection.aggregate-let-option>` option.
.. |let-option| replace:: :ref:`let <db.collection.aggregate-let-option>`

.. include:: /includes/let-variables-match-note.rst

.. include:: /includes/let-variables-example.rst

Expand Down
11 changes: 8 additions & 3 deletions source/reference/method/db.collection.findAndModify.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ Definition

.. include:: /includes/let-variables-syntax.rst

For an example, see
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`db.collection.findAndModify-let-example`.

.. versionadded:: 5.0
Expand Down Expand Up @@ -772,9 +774,12 @@ The operation returns the *updated* document:

.. _db.collection.findAndModify-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <db.collection.findAndModify-let-syntax>`.
.. |let-option| replace:: :ref:`let <db.collection.findAndModify-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-find-modify-flavors.rst

Expand Down
12 changes: 9 additions & 3 deletions source/reference/method/db.collection.remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ Definition

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`db.collection.remove-let-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`db.collection.remove-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -271,9 +274,12 @@ option:

.. _db.collection.remove-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <db.collection.remove-let-syntax>`.
.. |let-option| replace:: :ref:`let <db.collection.remove-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-delete-flavors.rst

Expand Down
12 changes: 9 additions & 3 deletions source/reference/method/db.collection.update.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ parameters:

.. include:: /includes/let-variables-syntax.rst

For an example, see :ref:`db.collection.update-let-example`.
.. include:: /includes/let-variables-syntax-note.rst

For a complete MQL example using ``let`` and variables, see
:ref:`db.collection.update-let-example`.

.. versionadded:: 5.0

Expand Down Expand Up @@ -1462,9 +1465,12 @@ does not modify the documents.

.. _db.collection.update-let-example:

.. include:: /includes/let-example-introduction.rst
Use Variables in ``let``
~~~~~~~~~~~~~~~~~~~~~~~~

For the syntax, see :ref:`let <db.collection.update-let-syntax>`.
.. |let-option| replace:: :ref:`let <db.collection.update-let-syntax>`

.. include:: /includes/let-example-introduction.rst

.. include:: /includes/let-example-update-flavors.rst

Expand Down