Skip to content

Commit 0f7528b

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-12942 numeric array indexing project (#521) (#539)
* 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 49ade4d commit 0f7528b

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
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: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,9 @@ Definition
5353

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

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

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

6661
- .. versionadded:: 3.4
@@ -102,8 +97,6 @@ must explicitly specify the suppression of the ``_id`` field in
10297
Exclude Fields
10398
~~~~~~~~~~~~~~
10499

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

@@ -119,11 +112,9 @@ not apply to conditional exclusion of a field using the
119112
Exclude Fields Conditionally
120113
````````````````````````````
121114

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

128119
Add New Fields or Reset Existing Fields
129120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -155,12 +146,13 @@ existing field, you can effectively rename a field.
155146
New Array Fields
156147
````````````````
157148

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

155+
.. include:: /includes/project-stage-and-array-index.rst
164156

165157
Embedded Document Fields
166158
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -185,10 +177,10 @@ embedded document to specify the field, e.g. ``contact: {
185177
Restrictions
186178
~~~~~~~~~~~~
187179

188-
.. versionchanged:: 3.4
180+
An error is returned if the :pipeline:`$project` specification is
181+
an empty document.
189182

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
192184

193185
Examples
194186
--------
@@ -258,8 +250,6 @@ The operation results in the following document:
258250
Exclude Fields from Output Documents
259251
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
260252

261-
.. versionadded:: 3.4
262-
263253
Consider a ``books`` collection with the following document:
264254

265255
.. code-block:: javascript
@@ -283,8 +273,6 @@ field from the output:
283273
Exclude Fields from Embedded Documents
284274
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285275

286-
.. versionadded:: 3.4
287-
288276
Consider a ``books`` collection with the following document:
289277

290278
.. code-block:: javascript
@@ -330,10 +318,8 @@ Both specifications result in the same output:
330318
Conditionally Exclude Fields
331319
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
332320

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

338324
Consider a ``books`` collection with the following document:
339325

@@ -526,6 +512,54 @@ The operation returns the following document:
526512

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

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

Comments
 (0)