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