|
| 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