Skip to content

Commit bf521ad

Browse files
author
Dave Cuthbert
authored
DOCSP-22725 BACKPORT (#1239)
1 parent bafa9a1 commit bf521ad

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Aggregation expressions accept a variable number of arguments. These
2+
arguments are normally passed as an array. However, when the argument
3+
is a single value, you can simplify your code by passing the argument
4+
directly without wrapping it in an array.
5+

source/reference/operator/aggregation/isArray.txt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _agg-expr-isArray:
2+
13
======================
24
$isArray (aggregation)
35
======================
@@ -34,47 +36,61 @@ The ``<expression>`` can be any valid :ref:`expression
3436

3537
.. list-table::
3638
:header-rows: 1
37-
:widths: 90 10
3839

3940
* - Example
4041
- Results
42+
- Notes
43+
44+
* - ``{ $isArray: "hello" }``
45+
- ``false``
46+
- ``"hello"`` is a string, passed as a string.
4147

4248
* - ``{ $isArray: [ "hello" ] }``
4349
- ``false``
50+
- ``"hello"`` is a string, passed as part of an argument array.
4451

45-
* - ``{ $isArray: [ [ "hello", "world" ] ] }``
52+
* - ``{ $isArray: [ [ "hello" ] ] }``
4653
- ``true``
54+
- ``[ "hello" ]`` is an array, passed as part of an argument array.
55+
56+
.. note::
57+
58+
.. include:: /includes/aggregation/fact-arrays-in-arguments.rst
4759

4860
Example
4961
-------
5062

51-
A collection named ``warehouses`` contains the following documents:
63+
Create the ``warehouses`` collection:
5264

5365
.. code-block:: javascript
5466

55-
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }
56-
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] }
57-
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }
58-
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
67+
db.warehouses.insertMany( [
68+
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
69+
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
70+
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
71+
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
72+
] )
5973

60-
The following example checks if the ``instock`` and the ``ordered``
61-
fields are arrays before concatenating the two:
74+
Check if the ``instock`` and the ``ordered`` fields are arrays. If both
75+
fields are arrays, concatenate them:
6276

6377
.. code-block:: javascript
6478

65-
db.warehouses.aggregate([
79+
db.warehouses.aggregate( [
6680
{ $project:
6781
{ items:
6882
{ $cond:
6983
{
70-
if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] },
84+
if: { $and: [ { $isArray: "$instock" },
85+
{ $isArray: "$ordered" }
86+
] },
7187
then: { $concatArrays: [ "$instock", "$ordered" ] },
7288
else: "One or more fields is not an array."
7389
}
7490
}
7591
}
7692
}
77-
])
93+
] )
7894

7995
.. code-block:: javascript
8096

0 commit comments

Comments
 (0)