@@ -54,14 +54,9 @@ Definition
54
54
55
55
- Adds a new field or resets the value of an existing field.
56
56
57
- .. versionchanged:: 3.6
57
+ If the the expression evaluates to ``$$REMOVE``, the field is
58
+ excluded in the output. For details, see :ref:`remove-var`.
58
59
59
- MongoDB 3.6 adds the variable :variable:`REMOVE`. If the
60
- the expression evaluates to ``$$REMOVE``, the field is
61
- excluded in the output. For details, see :ref:`remove-var`.
62
-
63
-
64
-
65
60
* - ``<field>:<0 or false>``
66
61
67
62
- Specifies the exclusion of a field.
@@ -103,8 +98,6 @@ must explicitly specify the suppression of the ``_id`` field in
103
98
Exclude Fields
104
99
~~~~~~~~~~~~~~
105
100
106
- .. versionadded:: 3.4
107
-
108
101
If you specify the exclusion of a field or fields, all other fields are
109
102
returned in the output documents.
110
103
@@ -126,11 +119,9 @@ See also the :pipeline:`$unset` stage to exclude fields.
126
119
Exclude Fields Conditionally
127
120
````````````````````````````
128
121
129
- .. versionadded:: 3.6
130
-
131
- Starting in MongoDB 3.6, you can use the variable :variable:`REMOVE` in
132
- aggregation expressions to conditionally suppress a field. For an
133
- example, see :ref:`remove-example`.
122
+ You can use the variable :variable:`REMOVE` in aggregation expressions
123
+ to conditionally suppress a field. For an example, see
124
+ :ref:`remove-example`.
134
125
135
126
Add New Fields or Reset Existing Fields
136
127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -162,12 +153,13 @@ existing field, you can effectively rename a field.
162
153
New Array Fields
163
154
````````````````
164
155
165
- Starting in MongoDB 3.2, :pipeline:`$project` stage supports using the
166
- square brackets ``[]`` to directly create new array fields. If array
167
- specification includes fields that are non-existent in a document, the
168
- operation substitutes ``null`` as the value for that field. For an
169
- example, see :ref:`example-project-new-array-fields`.
156
+ The :pipeline:`$project` stage supports using the square brackets ``[]``
157
+ to directly create new array fields. If you specify array fields that do
158
+ not exist in a document, the operation substitutes ``null`` as the value
159
+ for that field. For an example, see
160
+ :ref:`example-project-new-array-fields`.
170
161
162
+ .. include:: /includes/project-stage-and-array-index.rst
171
163
172
164
Embedded Document Fields
173
165
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -216,10 +208,10 @@ fails with the same error:
216
208
Restrictions
217
209
~~~~~~~~~~~~
218
210
219
- .. versionchanged:: 3.4
211
+ An error is returned if the :pipeline:`$project` specification is
212
+ an empty document.
220
213
221
- MongoDB 3.4 and later produces an error if the :pipeline:`$project`
222
- specification is an empty document.
214
+ .. include:: /includes/project-stage-and-array-index.rst
223
215
224
216
Examples
225
217
--------
@@ -289,9 +281,6 @@ The operation results in the following document:
289
281
Exclude Fields from Output Documents
290
282
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
283
292
-
293
- .. versionadded:: 3.4
294
-
295
284
Consider a ``books`` collection with the following document:
296
285
297
286
.. code-block:: javascript
@@ -317,8 +306,6 @@ See also the :pipeline:`$unset` stage to exclude fields.
317
306
Exclude Fields from Embedded Documents
318
307
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319
308
320
- .. versionadded:: 3.4
321
-
322
309
Consider a ``books`` collection with the following document:
323
310
324
311
.. code-block:: javascript
@@ -366,10 +353,8 @@ See also the :pipeline:`$unset` stage to exclude fields.
366
353
Conditionally Exclude Fields
367
354
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368
355
369
- .. versionadded:: 3.6
370
-
371
- Starting in MongoDB 3.6, you can use the variable :variable:`REMOVE` in
372
- aggregation expressions to conditionally suppress a field.
356
+ You can use the variable :variable:`REMOVE` in aggregation expressions
357
+ to conditionally suppress a field.
373
358
374
359
Consider a ``books`` collection with the following document:
375
360
@@ -560,6 +545,53 @@ The operation returns the following document:
560
545
561
546
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1, null ] }
562
547
548
+ .. _example-project-array-indexes:
549
+
550
+ Array Indexes are Unsupported
551
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
552
+
553
+ You cannot use an array index with the :pipeline:`$project` stage. This
554
+ section shows an example.
555
+
556
+ Create the following ``pizzas`` collection:
557
+
558
+ .. code-block:: javascript
559
+
560
+ db.pizzas.insert( [
561
+ { _id: 0, name: [ 'Pepperoni' ] },
562
+ ] )
563
+
564
+ The following example returns the pizza:
565
+
566
+ .. code-block:: javascript
567
+
568
+ db.pizzas.aggregate( [
569
+ { $project: { x: '$name', _id: 0 } },
570
+ ] )
571
+
572
+ The pizza is returned in the example output:
573
+
574
+ .. code-block:: javascript
575
+ :copyable: false
576
+
577
+ [ { x: [ 'Pepperoni' ] } ]
578
+
579
+ The following example uses an array index (``$name.0``) to attempt to
580
+ return the pizza:
581
+
582
+ .. code-block:: javascript
583
+
584
+ db.pizzas.aggregate( [
585
+ { $project: { x: '$name.0', _id: 0 } },
586
+ ] )
587
+
588
+ The pizza is not returned in the example output:
589
+
590
+ .. code-block:: javascript
591
+ :copyable: false
592
+
593
+ [ { x: [] } ]
594
+
563
595
.. seealso::
564
596
565
597
- :doc:`/tutorial/aggregation-zip-code-data-set`
0 commit comments