|
| 1 | +.. _agg-expr-isArray: |
| 2 | + |
1 | 3 | ======================
|
2 | 4 | $isArray (aggregation)
|
3 | 5 | ======================
|
@@ -32,47 +34,61 @@ The ``<expression>`` can be any valid :ref:`expression
|
32 | 34 |
|
33 | 35 | .. list-table::
|
34 | 36 | :header-rows: 1
|
35 |
| - :widths: 90 10 |
36 | 37 |
|
37 | 38 | * - Example
|
38 | 39 | - Results
|
| 40 | + - Notes |
| 41 | + |
| 42 | + * - ``{ $isArray: "hello" }`` |
| 43 | + - ``false`` |
| 44 | + - ``"hello"`` is a string, passed as a string. |
39 | 45 |
|
40 | 46 | * - ``{ $isArray: [ "hello" ] }``
|
41 | 47 | - ``false``
|
| 48 | + - ``"hello"`` is a string, passed as part of an argument array. |
42 | 49 |
|
43 |
| - * - ``{ $isArray: [ [ "hello", "world" ] ] }`` |
| 50 | + * - ``{ $isArray: [ [ "hello" ] ] }`` |
44 | 51 | - ``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 |
45 | 57 |
|
46 | 58 | Example
|
47 | 59 | -------
|
48 | 60 |
|
49 |
| -A collection named ``warehouses`` contains the following documents: |
| 61 | +Create the ``warehouses`` collection: |
50 | 62 |
|
51 | 63 | .. code-block:: javascript
|
52 | 64 |
|
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 | + ] ) |
57 | 71 |
|
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: |
60 | 74 |
|
61 | 75 | .. code-block:: javascript
|
62 | 76 |
|
63 |
| - db.warehouses.aggregate([ |
| 77 | + db.warehouses.aggregate( [ |
64 | 78 | { $project:
|
65 | 79 | { items:
|
66 | 80 | { $cond:
|
67 | 81 | {
|
68 |
| - if: { $and: [ { $isArray: "$instock" }, { $isArray: "$ordered" } ] }, |
| 82 | + if: { $and: [ { $isArray: "$instock" }, |
| 83 | + { $isArray: "$ordered" } |
| 84 | + ] }, |
69 | 85 | then: { $concatArrays: [ "$instock", "$ordered" ] },
|
70 | 86 | else: "One or more fields is not an array."
|
71 | 87 | }
|
72 | 88 | }
|
73 | 89 | }
|
74 | 90 | }
|
75 |
| - ]) |
| 91 | + ] ) |
76 | 92 |
|
77 | 93 | .. code-block:: javascript
|
78 | 94 |
|
|
0 commit comments