Skip to content

Commit 047f894

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-12942 numeric array indexing project (#521) (#537)
* DOCSP-12942-numeric-array-indexing-project * DOCSP-12942-numeric-array-indexing-project * DOCSP-12942-numeric-array-indexing-project * DOCSP-12942-numeric-array-indexing-project Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 9718af1 commit 047f894

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
You cannot use an array index with the :pipeline:`$project` stage.
2+
See :ref:`example-project-array-indexes`.

source/reference/operator/aggregation/project.txt

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,9 @@ Definition
5454

5555
- Adds a new field or resets the value of an existing field.
5656

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`.
5859

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-
6560
* - ``<field>:<0 or false>``
6661

6762
- Specifies the exclusion of a field.
@@ -103,8 +98,6 @@ must explicitly specify the suppression of the ``_id`` field in
10398
Exclude Fields
10499
~~~~~~~~~~~~~~
105100

106-
.. versionadded:: 3.4
107-
108101
If you specify the exclusion of a field or fields, all other fields are
109102
returned in the output documents.
110103

@@ -126,11 +119,9 @@ See also the :pipeline:`$unset` stage to exclude fields.
126119
Exclude Fields Conditionally
127120
````````````````````````````
128121

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`.
134125

135126
Add New Fields or Reset Existing Fields
136127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -162,12 +153,13 @@ existing field, you can effectively rename a field.
162153
New Array Fields
163154
````````````````
164155

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`.
170161

162+
.. include:: /includes/project-stage-and-array-index.rst
171163

172164
Embedded Document Fields
173165
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -216,10 +208,10 @@ fails with the same error:
216208
Restrictions
217209
~~~~~~~~~~~~
218210

219-
.. versionchanged:: 3.4
211+
An error is returned if the :pipeline:`$project` specification is
212+
an empty document.
220213

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
223215

224216
Examples
225217
--------
@@ -289,9 +281,6 @@ The operation results in the following document:
289281
Exclude Fields from Output Documents
290282
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291283

292-
293-
.. versionadded:: 3.4
294-
295284
Consider a ``books`` collection with the following document:
296285

297286
.. code-block:: javascript
@@ -317,8 +306,6 @@ See also the :pipeline:`$unset` stage to exclude fields.
317306
Exclude Fields from Embedded Documents
318307
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
319308

320-
.. versionadded:: 3.4
321-
322309
Consider a ``books`` collection with the following document:
323310

324311
.. code-block:: javascript
@@ -366,10 +353,8 @@ See also the :pipeline:`$unset` stage to exclude fields.
366353
Conditionally Exclude Fields
367354
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368355

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

374359
Consider a ``books`` collection with the following document:
375360

@@ -560,6 +545,53 @@ The operation returns the following document:
560545

561546
{ "_id" : ObjectId("55ad167f320c6be244eb3b95"), "myArray" : [ 1, 1, null ] }
562547

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+
563595
.. seealso::
564596

565597
- :doc:`/tutorial/aggregation-zip-code-data-set`

0 commit comments

Comments
 (0)