Skip to content

Commit 097cff1

Browse files
author
Dave Cuthbert
authored
DOCSP-22725 BACKPORT (#1238)
1 parent 7785a69 commit 097cff1

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
======================
@@ -32,47 +34,61 @@ The ``<expression>`` can be any valid :ref:`expression
3234

3335
.. list-table::
3436
:header-rows: 1
35-
:widths: 90 10
3637

3738
* - Example
3839
- Results
40+
- Notes
41+
42+
* - ``{ $isArray: "hello" }``
43+
- ``false``
44+
- ``"hello"`` is a string, passed as a string.
3945

4046
* - ``{ $isArray: [ "hello" ] }``
4147
- ``false``
48+
- ``"hello"`` is a string, passed as part of an argument array.
4249

43-
* - ``{ $isArray: [ [ "hello", "world" ] ] }``
50+
* - ``{ $isArray: [ [ "hello" ] ] }``
4451
- ``true``
52+
- ``[ "hello" ]`` is an array, passed as part of an argument array.
53+
54+
.. note::
55+
56+
.. include:: /includes/aggregation/fact-arrays-in-arguments.rst
4557

4658
Example
4759
-------
4860

49-
A collection named ``warehouses`` contains the following documents:
61+
Create the ``warehouses`` collection:
5062

5163
.. code-block:: javascript
5264

53-
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] }
54-
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] }
55-
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] }
56-
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
65+
db.warehouses.insertMany( [
66+
{ "_id" : 1, instock: [ "chocolate" ], ordered: [ "butter", "apples" ] },
67+
{ "_id" : 2, instock: [ "apples", "pudding", "pie" ] },
68+
{ "_id" : 3, instock: [ "pears", "pecans"], ordered: [ "cherries" ] },
69+
{ "_id" : 4, instock: [ "ice cream" ], ordered: [ ] }
70+
] )
5771

58-
The following example checks if the ``instock`` and the ``ordered``
59-
fields are arrays before concatenating the two:
72+
Check if the ``instock`` and the ``ordered`` fields are arrays. If both
73+
fields are arrays, concatenate them:
6074

6175
.. code-block:: javascript
6276

63-
db.warehouses.aggregate([
77+
db.warehouses.aggregate( [
6478
{ $project:
6579
{ items:
6680
{ $cond:
6781
{
68-
if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] },
82+
if: { $and: [ { $isArray: "$instock" },
83+
{ $isArray: "$ordered" }
84+
] },
6985
then: { $concatArrays: [ "$instock", "$ordered" ] },
7086
else: "One or more fields is not an array."
7187
}
7288
}
7389
}
7490
}
75-
])
91+
] )
7692

7793
.. code-block:: javascript
7894

0 commit comments

Comments
 (0)