@@ -53,14 +53,9 @@ Definition
53
53
54
54
- Adds a new field or resets the value of an existing field.
55
55
56
- .. versionchanged:: 3.6
56
+ If the the expression evaluates to ``$$REMOVE``, the field is
57
+ excluded in the output. For details, see :ref:`remove-var`.
57
58
58
- MongoDB 3.6 adds the variable :variable:`REMOVE`. If the
59
- the expression evaluates to ``$$REMOVE``, the field is
60
- excluded in the output. For details, see :ref:`remove-var`.
61
-
62
-
63
-
64
59
* - ``<field>:<0 or false>``
65
60
66
61
- .. versionadded:: 3.4
@@ -102,8 +97,6 @@ must explicitly specify the suppression of the ``_id`` field in
102
97
Exclude Fields
103
98
~~~~~~~~~~~~~~
104
99
105
- .. versionadded:: 3.4
106
-
107
100
If you specify the exclusion of a field or fields, all other fields are
108
101
returned in the output documents.
109
102
@@ -119,11 +112,9 @@ not apply to conditional exclusion of a field using the
119
112
Exclude Fields Conditionally
120
113
````````````````````````````
121
114
122
- .. versionadded:: 3.6
123
-
124
- Starting in MongoDB 3.6, you can use the variable :variable:`REMOVE` in
125
- aggregation expressions to conditionally suppress a field. For an
126
- example, see :ref:`remove-example`.
115
+ You can use the variable :variable:`REMOVE` in aggregation expressions
116
+ to conditionally suppress a field. For an example, see
117
+ :ref:`remove-example`.
127
118
128
119
Add New Fields or Reset Existing Fields
129
120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -155,12 +146,13 @@ existing field, you can effectively rename a field.
155
146
New Array Fields
156
147
````````````````
157
148
158
- Starting in MongoDB 3.2, :pipeline:`$project` stage supports using the
159
- square brackets ``[]`` to directly create new array fields. If array
160
- specification includes fields that are non-existent in a document, the
161
- operation substitutes ``null`` as the value for that field. For an
162
- example, see :ref:`example-project-new-array-fields`.
149
+ The :pipeline:`$project` stage supports using the square brackets ``[]``
150
+ to directly create new array fields. If you specify array fields that do
151
+ not exist in a document, the operation substitutes ``null`` as the value
152
+ for that field. For an example, see
153
+ :ref:`example-project-new-array-fields`.
163
154
155
+ .. include:: /includes/project-stage-and-array-index.rst
164
156
165
157
Embedded Document Fields
166
158
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -185,10 +177,10 @@ embedded document to specify the field, e.g. ``contact: {
185
177
Restrictions
186
178
~~~~~~~~~~~~
187
179
188
- .. versionchanged:: 3.4
180
+ An error is returned if the :pipeline:`$project` specification is
181
+ an empty document.
189
182
190
- MongoDB 3.4 and later produces an error if the :pipeline:`$project`
191
- specification is an empty document.
183
+ .. include:: /includes/project-stage-and-array-index.rst
192
184
193
185
Examples
194
186
--------
@@ -258,8 +250,6 @@ The operation results in the following document:
258
250
Exclude Fields from Output Documents
259
251
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
260
252
261
- .. versionadded:: 3.4
262
-
263
253
Consider a ``books`` collection with the following document:
264
254
265
255
.. code-block:: javascript
@@ -283,8 +273,6 @@ field from the output:
283
273
Exclude Fields from Embedded Documents
284
274
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285
275
286
- .. versionadded:: 3.4
287
-
288
276
Consider a ``books`` collection with the following document:
289
277
290
278
.. code-block:: javascript
@@ -330,10 +318,8 @@ Both specifications result in the same output:
330
318
Conditionally Exclude Fields
331
319
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332
320
333
- .. versionadded:: 3.6
334
-
335
- Starting in MongoDB 3.6, you can use the variable :variable:`REMOVE` in
336
- aggregation expressions to conditionally suppress a field.
321
+ You can use the variable :variable:`REMOVE` in aggregation expressions
322
+ to conditionally suppress a field.
337
323
338
324
Consider a ``books`` collection with the following document:
339
325
@@ -526,6 +512,54 @@ The operation returns the following document:
526
512
527
513
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1, null ] }
528
514
529
- .. seealso::
530
- :doc:`/tutorial/aggregation-zip-code-data-set`,
531
- :doc:`/tutorial/aggregation-with-user-preference-data`
515
+ .. _example-project-array-indexes:
516
+
517
+ Array Indexes are Unsupported
518
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
519
+
520
+ You cannot use an array index with the :pipeline:`$project` stage. This
521
+ section shows an example.
522
+
523
+ Create the following ``pizzas`` collection:
524
+
525
+ .. code-block:: javascript
526
+
527
+ db.pizzas.insert( [
528
+ { _id: 0, name: [ 'Pepperoni' ] },
529
+ ] )
530
+
531
+ The following example returns the pizza:
532
+
533
+ .. code-block:: javascript
534
+
535
+ db.pizzas.aggregate( [
536
+ { $project: { x: '$name', _id: 0 } },
537
+ ] )
538
+
539
+ The pizza is returned in the example output:
540
+
541
+ .. code-block:: javascript
542
+ :copyable: false
543
+
544
+ [ { x: [ 'Pepperoni' ] } ]
545
+
546
+ The following example uses an array index (``$name.0``) to attempt to
547
+ return the pizza:
548
+
549
+ .. code-block:: javascript
550
+
551
+ db.pizzas.aggregate( [
552
+ { $project: { x: '$name.0', _id: 0 } },
553
+ ] )
554
+
555
+ The pizza is not returned in the example output:
556
+
557
+ .. code-block:: javascript
558
+ :copyable: false
559
+
560
+ [ { x: [] } ]
561
+
562
+ .. seealso::
563
+
564
+ - :doc:`/tutorial/aggregation-zip-code-data-set`
565
+ - :doc:`/tutorial/aggregation-with-user-preference-data`
0 commit comments