Skip to content

Commit 026715e

Browse files
jason-price-mongodbandf-mongodb
authored andcommitted
DOCS-13620 Let variables in update
1 parent 76561f8 commit 026715e

15 files changed

+375
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Create a collection ``cakeFlavors``:
2+
3+
.. code-block:: javascript
4+
5+
db.cakeFlavors.insert( [
6+
{ _id: 1, flavor: "chocolate" },
7+
{ _id: 2, flavor: "strawberry" },
8+
{ _id: 3, flavor: "cherry" }
9+
] )
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. include:: /includes/let-example-create-flavors.rst
2+
3+
The following example defines a ``targetFlavor`` variable in ``let`` and
4+
uses the variable to delete the strawberry cake flavor:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. include:: /includes/let-example-create-flavors.rst
2+
3+
The following example defines a ``targetFlavor`` variable in ``let`` and
4+
uses the variable to retrieve the chocolate cake flavor:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.. include:: /includes/let-example-create-flavors.rst
2+
3+
The following example defines a ``targetFlavor`` variable in ``let`` and
4+
uses the variable to change the cake flavor from cherry to orange:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Use Variables in ``let``
2+
~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
.. versionadded:: 5.0
5+
6+
To specify variables that can be accessed in the command, use the
7+
``let`` option.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. include:: /includes/let-example-create-flavors.rst
2+
3+
The following example defines ``targetFlavor`` and ``newFlavor``
4+
variables in ``let`` and uses the variables to change the cake flavor
5+
from cherry to orange:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Specifies a document with a list of variables. This allows you to
2+
improve command readability by separating the variables from the query
3+
text. The document syntax is:
4+
5+
.. code-block:: javascript
6+
:copyable: false
7+
8+
{ <variable_name_1>: <expression_1>,
9+
...,
10+
<variable_name_n>: <expression_n> }
11+
12+
The variable is set to the value returned by the expression, and cannot
13+
be changed afterwards.
14+
15+
Examples:
16+
17+
- ``{ targetTotal: 3000 }``, sets ``targetTotal`` to ``3000``.
18+
19+
- ``{ totalCost: { $add: [ 125, 16, 765 ] } }``, sets ``totalCost`` to
20+
``906``.
21+
22+
- ``{ myRecordingTime: "$$NOW" }``, sets ``myRecordingTime`` to the
23+
current date and time returned by the :variable:`NOW` system
24+
variable.
25+
26+
To access the value of a variable in the command, use the double
27+
dollar sign prefix (``$$``) together with your variable name in the form
28+
``$$<variable_name>``. For example: ``$$targetTotal``.

source/reference/command/delete.txt

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Definition
2121
collections </core/capped-collections>`. The remove methods provided
2222
by the MongoDB drivers use this command internally.
2323

24+
.. versionchanged:: 5.0
25+
26+
.. _delete-syntax:
27+
2428
The :dbcommand:`delete` command has the following syntax:
2529

2630
.. code-block:: javascript
@@ -38,7 +42,8 @@ Definition
3842
...
3943
],
4044
ordered: <boolean>,
41-
writeConcern: { <write concern> }
45+
writeConcern: { <write concern> },
46+
let: <document> // Added in MongoDB 5.0
4247
}
4348

4449
The command takes the following fields:
@@ -110,6 +115,20 @@ Definition
110115
.. versionadded:: 4.4
111116

112117

118+
* - :ref:`let <delete-let-syntax>`
119+
120+
- document
121+
122+
- .. _delete-let-syntax:
123+
124+
Optional.
125+
126+
.. include:: /includes/let-syntax-generic.rst
127+
128+
For an example, see :ref:`delete-let-example`.
129+
130+
.. versionadded:: 5.0
131+
113132

114133
.. |operation| replace:: delete
115134

@@ -414,6 +433,25 @@ To see the index used, run :dbcommand:`explain` on the operation:
414433
}
415434
)
416435

436+
.. _delete-let-example:
437+
438+
.. include:: /includes/let-example-introduction.rst
439+
440+
For the syntax, see :ref:`let <delete-let-syntax>`.
441+
442+
.. include:: /includes/let-example-delete-flavors.rst
443+
444+
.. code-block:: javascript
445+
446+
db.runCommand( {
447+
delete: db.cakeFlavors.getName(),
448+
deletes: [ {
449+
q: { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
450+
limit: 1
451+
} ],
452+
let : { targetFlavor: "strawberry" }
453+
} )
454+
417455
.. _delete-command-output:
418456

419457
Output

source/reference/command/find.txt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Syntax
4040

4141
The :dbcommand:`find` command has the following syntax:
4242

43+
.. versionchanged:: 5.0
44+
4345
.. code-block:: javascript
4446

4547
db.runCommand(
@@ -66,7 +68,8 @@ The :dbcommand:`find` command has the following syntax:
6668
"awaitData": <bool>,
6769
"allowPartialResults": <bool>,
6870
"collation": <document>,
69-
"allowDiskUse" : <bool>
71+
"allowDiskUse" : <bool>,
72+
"let": <document> // Added in MongoDB 5.0
7073
}
7174
)
7275

@@ -396,6 +399,22 @@ The command accepts the following fields:
396399

397400
.. versionadded:: 4.4
398401

402+
403+
404+
* - :ref:`let <find-let-syntax>`
405+
406+
- document
407+
408+
- .. _find-let-syntax:
409+
410+
Optional.
411+
412+
.. include:: /includes/let-syntax-generic.rst
413+
414+
For an example, see :ref:`find-let-example`.
415+
416+
.. versionadded:: 5.0
417+
399418
.. _cmd-find-output:
400419

401420
Output
@@ -611,4 +630,21 @@ The :binary:`~bin.mongo` shell provides the :method:`cursor.collation()` to
611630
specify :ref:`collation <collation>` for a
612631
:method:`db.collection.find()` operation.
613632

633+
.. _find-let-example:
634+
635+
.. include:: /includes/let-example-introduction.rst
636+
637+
For the syntax, see :ref:`let <find-let-syntax>`.
638+
639+
.. include:: /includes/let-example-find-flavors.rst
640+
641+
.. code-block:: javascript
642+
643+
db.cakeFlavors.runCommand( {
644+
find: db.cakeFlavors.getName(),
645+
filter: { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
646+
let : { targetFlavor: "chocolate" }
647+
} )
648+
649+
614650
.. seealso:: :ref:`3.2-driver-compatibility`

source/reference/command/findAndModify.txt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Definition
2424
modifications made on the update. To return the document with the
2525
modifications made on the update, use the ``new`` option.
2626

27+
.. versionchanged:: 5.0
28+
2729
The command has the following syntax:
2830

2931
.. code-block:: none
@@ -42,7 +44,8 @@ Definition
4244
collation: <document>,
4345
arrayFilters: <array>,
4446
hint: <document|string>,
45-
comment: <any>
47+
comment: <any>,
48+
let: <document> // Added in MongoDB 5.0
4649
}
4750

4851
The :dbcommand:`findAndModify` command takes the following
@@ -250,6 +253,19 @@ Definition
250253

251254
.. versionadded:: 4.4
252255

256+
* - :ref:`let <findAndModify-let-syntax>`
257+
258+
- document
259+
260+
- .. _findAndModify-let-syntax:
261+
262+
Optional.
263+
264+
.. include:: /includes/let-syntax-generic.rst
265+
266+
For an example, see :ref:`findAndModify-let-example`.
267+
268+
.. versionadded:: 5.0
253269

254270
Output
255271
------
@@ -1041,4 +1057,22 @@ To see the index used, run :dbcommand:`explain` on the operation:
10411057
}
10421058
)
10431059

1060+
.. _findAndModify-let-example:
1061+
1062+
.. include:: /includes/let-example-introduction.rst
1063+
1064+
For the syntax, see :ref:`let <findAndModify-let-syntax>`.
1065+
1066+
.. include:: /includes/let-example-find-modify-flavors.rst
1067+
1068+
.. code-block:: javascript
1069+
1070+
db.cakeFlavors.runCommand( {
1071+
findAndModify: db.cakeFlavors.getName(),
1072+
query: { $expr: { $eq: [ "$flavor", "$$targetFlavor" ] } },
1073+
update: { flavor: "orange" },
1074+
let: { targetFlavor: "cherry" }
1075+
} )
1076+
1077+
10441078
.. seealso:: :ref:`perform-findAndModify-linearizable-reads`

0 commit comments

Comments
 (0)