Skip to content

Commit 9c2b121

Browse files
jwilliams-mongojeff-allen-mongo
authored andcommitted
(DOCSP-17440)(DOCSP-16149): getField and setField aggregation expressions
1 parent 7a8074d commit 9c2b121

File tree

7 files changed

+633
-2
lines changed

7 files changed

+633
-2
lines changed

config/redirects

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,10 @@ raw: /manual/core/wildcard -> ${base}/manual/core/index-wildcard/
17931793

17941794
[v3.6-v4.4]: /${version}/reference/versioned-api/ -> ${base}/${version}/reference/
17951795

1796+
[v3.6-v4.4]: /${version}/reference/operator/aggregation/getField/ -> ${base}/${version}/reference/operator/aggregation/
1797+
1798+
[v3.6-v4.4]: /${version}/reference/operator/aggregation/setField/ -> ${base}/${version}/reference/operator/aggregation/
1799+
17961800
#
17971801
# Redirects for 5.0 and greater (if pages are removed in 4.4 that used to exist in earlier versions)
17981802
#

source/includes/extracts-agg-operators.yaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,15 @@ content: |
519519
* - Name
520520
- Description
521521
522+
* - :expression:`$getField`
523+
524+
- Returns the value of a specified field from a document.
525+
You can use :expression:`$getField` to retrieve the value of
526+
fields with names that contain periods (``.``) or start
527+
with dollar signs (``$``).
528+
529+
.. versionadded:: 5.0
530+
522531
* - :expression:`$rand`
523532
- Returns a random float between 0 and 1
524533
@@ -553,6 +562,15 @@ content: |
553562
554563
.. versionadded:: 3.6
555564
565+
* - :expression:`$setField`
566+
567+
- Adds, updates, or removes a specified field in a document. You
568+
can use :expression:`$setField` to add, update,
569+
or remove fields with names that contain periods (``.``) or
570+
start with dollar signs (``$``).
571+
572+
.. versionadded:: 5.0
573+
556574
---
557575
ref: agg-operators-set
558576
content: |
@@ -818,7 +836,8 @@ content: |
818836
- Return a value without parsing. Use for values that the
819837
aggregation pipeline may interpret as an expression. For
820838
example, use a :expression:`$literal` expression to a string
821-
that starts with a ``$`` to avoid parsing as a field path.
839+
that starts with a dollar sign (``$``) to avoid parsing as a
840+
field path.
822841
823842
---
824843
ref: agg-operators-custom-aggregation

source/meta/aggregation-quick-reference.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ Index of Expression Operators
378378
- :group:`$first` (accumulator)
379379
- :expression:`$floor`
380380
- :expression:`$function`
381+
- :expression:`$getField`
381382
- :expression:`$gt`
382383
- :expression:`$gte`
383384
- :expression:`$hour`
@@ -432,6 +433,7 @@ Index of Expression Operators
432433
- :expression:`$second`
433434
- :expression:`$setDifference`
434435
- :expression:`$setEquals`
436+
- :expression:`$setField`
435437
- :expression:`$setIntersection`
436438
- :expression:`$setIsSubset`
437439
- :expression:`$setUnion`

source/reference/operator/aggregation.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,15 @@ Alphabetical Listing of Expression Operators
493493

494494
.. versionadded:: 4.4
495495

496+
* - :expression:`$getField`
497+
498+
- Returns the value of a specified field from a document.
499+
You can use :expression:`$getField` to retrieve the value of
500+
fields with names that contain periods (``.``) or start
501+
with dollar signs (``$``).
502+
503+
.. versionadded: 5.0
504+
496505
* - :expression:`$gt`
497506

498507
- Returns ``true`` if the first value is
@@ -861,8 +870,16 @@ Alphabetical Listing of Expression Operators
861870

862871
- Returns ``true`` if the input sets have the same distinct elements.
863872
Accepts two or more argument expressions.
864-
865873

874+
* - :expression:`$setField`
875+
876+
- Adds, updates, or removes a specified field in a document.
877+
You can use :expression:`$setField` to add, update,
878+
or remove fields with names that contain periods (``.``) or
879+
start with dollar signs (``$``).
880+
881+
.. versionadded:: 5.0
882+
866883
* - :expression:`$setIntersection`
867884

868885
- Returns a set with elements that appear in *all* of the input sets.
@@ -1158,6 +1175,7 @@ Alphabetical Listing of Expression Operators
11581175
/reference/operator/aggregation/first-array-element
11591176
/reference/operator/aggregation/floor
11601177
/reference/operator/aggregation/function
1178+
/reference/operator/aggregation/getField
11611179
/reference/operator/aggregation/gt
11621180
/reference/operator/aggregation/gte
11631181
/reference/operator/aggregation/hour
@@ -1215,6 +1233,7 @@ Alphabetical Listing of Expression Operators
12151233
/reference/operator/aggregation/second
12161234
/reference/operator/aggregation/setDifference
12171235
/reference/operator/aggregation/setEquals
1236+
/reference/operator/aggregation/setField
12181237
/reference/operator/aggregation/setIntersection
12191238
/reference/operator/aggregation/setIsSubset
12201239
/reference/operator/aggregation/setUnion
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
=======================
2+
$getField (aggregation)
3+
=======================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. expression:: $getField
17+
18+
.. versionadded:: 5.0
19+
20+
Returns the value of a specified field from a document. If you
21+
don't specify an object, :expression:`$getField` returns the value of
22+
the field from :variable:`$$CURRENT <CURRENT>`.
23+
24+
You can use :expression:`$getField` to retrieve the value of fields
25+
with names that contain periods (``.``) or start with dollar signs
26+
(``$``).
27+
28+
.. tip::
29+
30+
Use :expression:`$setField` to add or update fields with names
31+
that contain dollar signs (``$``) or periods (``.``).
32+
33+
Syntax
34+
------
35+
36+
:expression:`$getField` has the following syntax:
37+
38+
.. code-block:: javascript
39+
40+
{
41+
$getField: {
42+
field: <String>,
43+
input: <Object>
44+
}
45+
}
46+
47+
.. list-table::
48+
:header-rows: 1
49+
:widths: 20 20 80
50+
51+
* - Field
52+
- Type
53+
- Description
54+
55+
* - ``field``
56+
- String
57+
- Field in the ``input`` object for which you want to return a
58+
value. ``field`` can be any valid :ref:`expression
59+
<aggregation-expressions>` that resolves to a string
60+
constant.
61+
62+
If ``field`` begins with a dollar sign (``$``), place the field
63+
name inside of a :expression:`$literal` expression to return its
64+
value.
65+
66+
* - ``input``
67+
- Object
68+
69+
*Default*: :variable:`$$CURRENT <CURRENT>`
70+
71+
A valid :ref:`expression <aggregation-expressions>` that
72+
contains the ``field`` for which you want to return a value.
73+
``input`` must resolve to an object, ``missing``,
74+
``null``, or ``undefined``. If omitted, defaults
75+
to the document currently being processed in the pipeline
76+
(:variable:`$$CURRENT <CURRENT>`).
77+
78+
:expression:`$getField` has the following shorthand syntax for
79+
retrieving field values from :variable:`$$CURRENT <CURRENT>`:
80+
81+
.. code-block:: javascript
82+
83+
{
84+
$getField: <String>
85+
}
86+
87+
For this syntax, the argument is equivalent to the value of ``field``
88+
described above.
89+
90+
Behavior
91+
--------
92+
93+
- If ``field`` resolves to anything other than a string constant,
94+
:expression:`$getField` returns an error.
95+
96+
- If the ``field`` that you specify is not present in the ``input``
97+
object, or in :variable:`$$CURRENT <CURRENT>` if you don't specify an
98+
``input`` object, :expression:`$getField` returns ``missing``.
99+
100+
- If ``input`` evaluates to ``missing``, ``undefined``, or ``null``,
101+
:expression:`$getField` returns ``null``.
102+
103+
- If ``input`` evaluates to anything other than an object, ``missing``,
104+
``undefined``, or ``null``, :expression:`$getField` returns an error.
105+
106+
- :expression:`$getField` doesn't implicitly traverse objects or arrays.
107+
For example, :expression:`$getField` evaluates a ``field`` value of
108+
``a.b.c`` as a top-level field ``a.b.c`` instead of a nested field
109+
``{ a: { b: { c: } } }``.
110+
111+
Examples
112+
--------
113+
114+
Query Fields that Contain Periods (``.``)
115+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116+
117+
Consider an ``inventory`` collection with the following documents:
118+
119+
.. code-block:: javascript
120+
121+
{ "_id" : 1, "item" : "sweatshirt", "price.usd": 45.99, qty: 300 }
122+
{ "_id" : 2, "item" : "winter coat", "price.usd": 499.99, qty: 200 }
123+
{ "_id" : 3, "item" : "sun dress", "price.usd": 199.99, qty: 250 }
124+
{ "_id" : 4, "item" : "leather boots", "price.usd": 249.99, qty: 300 }
125+
{ "_id" : 5, "item" : "bow tie", "price.usd": 9.99, qty: 180 }
126+
127+
The following operation uses the :expression:`$getField` and
128+
:expression:`$gt` operators to find which products have a ``price.usd``
129+
greater than ``200``:
130+
131+
.. code-block:: javascript
132+
133+
db.inventory.aggregate([
134+
{
135+
$match:
136+
{ $expr:
137+
{ $gt: [ { $getField: "price.usd" }, 200 ] }
138+
}
139+
}
140+
])
141+
142+
The operation returns the following results:
143+
144+
.. code-block:: javascript
145+
:copyable: false
146+
147+
[
148+
{ _id: 2, item: 'winter coat', qty: 200, 'price.usd': 499.99 },
149+
{ _id: 4, item: 'leather boots', qty: 300, 'price.usd': 249.99 }
150+
]
151+
152+
Query Fields that Start with a Dollar Sign (``$``)
153+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
154+
155+
Consider an ``inventory`` collection with the following documents:
156+
157+
.. code-block:: javascript
158+
159+
{ "_id" : 1, "item" : "sweatshirt", "$price": 45.99, qty: 300 }
160+
{ "_id" : 2, "item" : "winter coat", "$price": 499.99, qty: 200 }
161+
{ "_id" : 3, "item" : "sun dress", "$price": 199.99, qty: 250 }
162+
{ "_id" : 4, "item" : "leather boots", "$price": 249.99, qty: 300 }
163+
{ "_id" : 5, "item" : "bow tie", "$price": 9.99, qty: 180 }
164+
165+
The following operation uses the :expression:`$getField`,
166+
:expression:`$gt`, and :expression:`$literal` operators to find which
167+
products have a ``$price`` greater than ``200``:
168+
169+
.. code-block:: javascript
170+
171+
db.inventory.aggregate([
172+
{
173+
$match:
174+
{ $expr:
175+
{ $gt: [ { $getField: {$literal: "$price" } }, 200 ] }
176+
}
177+
}
178+
])
179+
180+
The operation returns the following results:
181+
182+
.. code-block:: javascript
183+
:copyable: false
184+
185+
[
186+
{ _id: 2, item: 'winter coat', qty: 200, '$price': 499.99 },
187+
{ _id: 4, item: 'leather boots', qty: 300, '$price': 249.99 }
188+
]

0 commit comments

Comments
 (0)