|
| 1 | +.. default-domain:: mongodb |
| 2 | + |
| 3 | +SQL to Aggregation Framework |
| 4 | +---------------------------- |
| 5 | + |
| 6 | +With the :doc:`/applications/aggregation`, MongoDB provides analogous |
| 7 | +functionality to common SQL aggregation functions. |
| 8 | + |
| 9 | +COUNT(*) |
| 10 | +~~~~~~~~ |
| 11 | + |
| 12 | +MongoDB provides three ways to perform the SQL operation of ``count(*)``: |
| 13 | + |
| 14 | +- the :dbcommand:`count()` command |
| 15 | +- the :method:`count() <cursor.count()>` method |
| 16 | +- the :agg:pipeline:`$group` operator in conjunction with the |
| 17 | + :agg:expression:`$sum` operator in the |
| 18 | + :doc:`/applications/aggregation`. |
| 19 | + |
| 20 | +Consider the following SQL command which performs a count of the rows |
| 21 | +in the ``orders`` table: |
| 22 | + |
| 23 | +.. code-block:: sql |
| 24 | + |
| 25 | + SELECT COUNT(*) AS count |
| 26 | + FROM orders |
| 27 | + |
| 28 | +In MongoDB, |
| 29 | + |
| 30 | +- the analogous query using the :dbcommand:`count()` command is: |
| 31 | + |
| 32 | +.. code-block:: javascript |
| 33 | + :emphasize-lines: 1 |
| 34 | + |
| 35 | + db.orders.count() |
| 36 | + |
| 37 | +- the analogous query using the :method:`count() <cursor.count()>` |
| 38 | + method is: |
| 39 | + |
| 40 | +.. code-block:: javascript |
| 41 | + :emphasize-lines: 1 |
| 42 | + |
| 43 | + db.orders.find().count() |
| 44 | + |
| 45 | +- the analogous query using the :doc:`/applications/aggregation` is: |
| 46 | + |
| 47 | +.. code-block:: javascript |
| 48 | + :emphasize-lines: 1 |
| 49 | + |
| 50 | + db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } } ] ) |
| 51 | + |
| 52 | +The :doc:`/applications/aggregation` query performs a |
| 53 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 54 | +operator to calculate the ``count`` with no grouping. Within the :agg:pipeline:`$group` |
| 55 | +operation, the :agg:expression:`$sum` operator calculates the ``count`` |
| 56 | +by adding ``1`` for each document in the ``orders`` collection as grouped by ``null``. |
| 57 | + |
| 58 | +SUM(<column>) |
| 59 | +~~~~~~~~~~~~~ |
| 60 | + |
| 61 | +MongoDB provides aggregation :agg:expression:`$sum` operator to perform the |
| 62 | +SQL operation of ``sum(<column>)``. |
| 63 | + |
| 64 | +Consider the following SQL command which sums the ``price`` column in |
| 65 | +the ``orders`` table: |
| 66 | + |
| 67 | +.. code-block:: sql |
| 68 | + |
| 69 | + SELECT SUM(price) AS total |
| 70 | + FROM orders |
| 71 | + |
| 72 | +In MongoDB, the analogous query using the |
| 73 | +:doc:`/applications/aggregation` is: |
| 74 | + |
| 75 | +.. code-block:: javascript |
| 76 | + :emphasize-lines: 1 |
| 77 | + |
| 78 | + db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } } ] ) |
| 79 | + |
| 80 | +The :doc:`/applications/aggregation` query performs a |
| 81 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 82 | +operator to calculate the ``total`` on the ``price`` field grouped by |
| 83 | +``null`` (i.e. with no grouping.) |
| 84 | + |
| 85 | +Note the ``$`` in front of the field ``price`` as it appears as an |
| 86 | +operand to the aggregation operators. |
| 87 | + |
| 88 | +GROUP BY(<column>...) |
| 89 | +~~~~~~~~~~~~~~~~~~~~~ |
| 90 | + |
| 91 | +MongoDB provides aggregation :agg:expression:`$group` operator to perform the |
| 92 | +SQL operation of ``GROUP BY ()``. |
| 93 | + |
| 94 | +*Grouping by single column* |
| 95 | + |
| 96 | +Consider the following SQL command which groups the ``price`` information by |
| 97 | +``cust_id``: |
| 98 | + |
| 99 | +.. code-block:: sql |
| 100 | + |
| 101 | + SELECT cust_id, SUM(price) AS total |
| 102 | + FROM orders |
| 103 | + GROUP BY cust_id |
| 104 | + |
| 105 | +In MongoDB, the analogous query using the |
| 106 | +:doc:`/applications/aggregation` is: |
| 107 | + |
| 108 | +.. code-block:: javascript |
| 109 | + :emphasize-lines: 1 |
| 110 | + |
| 111 | + db.orders.aggregate( [ { $group: { _id: "$cust_id" , total: { $sum: "$price" } } } ] ) |
| 112 | + |
| 113 | +The :doc:`/applications/aggregation` query performs a |
| 114 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 115 | +operator to calculate the ``total`` on the ``price`` field as grouped |
| 116 | +by the ``cust_id`` field. |
| 117 | + |
| 118 | +Note the ``$`` in front of the fields ``cust_id`` and ``price`` as they |
| 119 | +appear as operands to the aggregation operators. |
| 120 | + |
| 121 | +*Grouping by multiple columns* |
| 122 | + |
| 123 | +Consider the following SQL command which groups the ``price`` |
| 124 | +information by both ``cust_id`` and ``ord_date``: |
| 125 | + |
| 126 | +.. code-block:: sql |
| 127 | + |
| 128 | + SELECT cust_id, ord_date, SUM(price) AS total |
| 129 | + FROM orders |
| 130 | + GROUP BY cust_id, ord_date |
| 131 | + |
| 132 | +In MongoDB, the analogous query using the |
| 133 | +:doc:`/applications/aggregation` is: |
| 134 | + |
| 135 | +.. code-block:: javascript |
| 136 | + :emphasize-lines: 1 |
| 137 | + |
| 138 | + db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, total: { $sum: "$price" } } } ] ) |
| 139 | + |
| 140 | +The :doc:`/applications/aggregation` query performs the |
| 141 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 142 | +operator to calculate the ``total`` on the ``price`` field as grouped |
| 143 | +by the ``cust_id`` and ``ord_date`` fields. |
| 144 | + |
| 145 | +Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and |
| 146 | +``price`` as they appear as operands to the aggregation operators. |
| 147 | + |
| 148 | +GROUP BY ... HAVING ... |
| 149 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +MongoDB provides aggregation :agg:expression:`$group` and |
| 152 | +:agg:expression:`$match` operators to perform the SQL operation of |
| 153 | +``GROUP BY ... HAVING ...``. |
| 154 | + |
| 155 | +*Grouping by single column ... having* |
| 156 | + |
| 157 | +Consider the following SQL command which can be used to find the |
| 158 | +``cust_id`` 's with multiple rows: |
| 159 | + |
| 160 | +.. code-block:: sql |
| 161 | + |
| 162 | + SELECT cust_id, count(*) |
| 163 | + FROM orders |
| 164 | + GROUP BY cust_id |
| 165 | + HAVING count(*) > 1 |
| 166 | + |
| 167 | +In MongoDB, the analogous query using the |
| 168 | +:doc:`/applications/aggregation` is: |
| 169 | + |
| 170 | +.. code-block:: javascript |
| 171 | + :emphasize-lines: 2-3 |
| 172 | + |
| 173 | + db.orders.aggregate( [ |
| 174 | + { $group: { _id: "$cust_id", count: { $sum: 1 } } }, |
| 175 | + { $match: { count: { $gt: 1 } } } |
| 176 | + ] ) |
| 177 | + |
| 178 | +The :doc:`/applications/aggregation` query first performs a |
| 179 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 180 | +operator to calculate the ``count`` of the documents grouped by |
| 181 | +``cust_id``. Then the query pipes the results to the |
| 182 | +:agg:expression:`$match` operator that returns only those results that |
| 183 | +have ``count`` greater than ``1``. |
| 184 | + |
| 185 | +Note the ``$`` in front of the fields ``cust_id`` as it appear as an |
| 186 | +operand to the aggregation operators. |
| 187 | + |
| 188 | +*Grouping by multiple columns ... having* |
| 189 | + |
| 190 | +Consider the following SQL command which can be used to find the |
| 191 | +calculated column ``total`` greater than ``250`` grouped by ``cust_id`` and |
| 192 | +``ord_date``: |
| 193 | + |
| 194 | +.. code-block:: sql |
| 195 | + |
| 196 | + SELECT cust_id, ord_date, SUM(price) AS total |
| 197 | + FROM orders |
| 198 | + GROUP BY cust_id, ord_date |
| 199 | + HAVING total > 250 |
| 200 | + |
| 201 | +In MongoDB, the analogous query using the |
| 202 | +:doc:`/applications/aggregation` is: |
| 203 | + |
| 204 | +.. code-block:: javascript |
| 205 | + :emphasize-lines: 2-3 |
| 206 | + |
| 207 | + db.orders.aggregate( [ |
| 208 | + { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, total: { $sum: "$price" } } }, |
| 209 | + { $match: { total: { $gt: 250 } } } |
| 210 | + ] ) |
| 211 | + |
| 212 | +The :doc:`/applications/aggregation` query first performs a |
| 213 | +:agg:pipeline:`$group` operation that uses the :agg:expression:`$sum` |
| 214 | +operator to calculate the ``total`` on the ``price`` field as grouped |
| 215 | +by the ``cust_id`` and ``ord_date`` fields. Then the query pipes the |
| 216 | +results to the :agg:expression:`$match` operator that returns only |
| 217 | +those results that have ``total`` greater than ``250``. |
| 218 | + |
| 219 | +Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and |
| 220 | +``price`` as they appear as operands to the aggregation operators. |
| 221 | + |
| 222 | +WHERE ... GROUP BY |
| 223 | +~~~~~~~~~~~~~~~~~~ |
| 224 | +MongoDB provides aggregation :agg:expression:`$group` and |
| 225 | +:agg:expression:`$match` operators to perform the SQL operation of |
| 226 | +``WHERE ... GROUP BY``. |
| 227 | + |
| 228 | +Consider the following SQL command which can be used to find the |
| 229 | +``cust_id`` and the calculated column ``total`` for orders with status |
| 230 | +``A`` grouped by ``cust_id``: |
| 231 | + |
| 232 | +.. code-block:: sql |
| 233 | + |
| 234 | + SELECT cust_id, SUM(price) as total |
| 235 | + FROM orders |
| 236 | + WHERE status = 'A' |
| 237 | + GROUP BY cust_id |
| 238 | + |
| 239 | +In MongoDB, the analogous query using the |
| 240 | +:doc:`/applications/aggregation` is: |
| 241 | + |
| 242 | +.. code-block:: javascript |
| 243 | + :emphasize-lines: 2-3 |
| 244 | + |
| 245 | + db.orders.aggregate( [ |
| 246 | + { $match: { status: 'A' } }, |
| 247 | + { $group: { _id: "$cust_id", total: { $sum: "$price" } } } |
| 248 | + ] ) |
| 249 | + |
| 250 | +The :doc:`/applications/aggregation` query first performs a |
| 251 | +:agg:expression:`$match` operation that returns only those documents |
| 252 | +that have ``status`` field equal to ``A``. Then the query pipes the |
| 253 | +results to the :agg:pipeline:`$group` operation that uses the |
| 254 | +:agg:expression:`$sum` operator to calculate the ``total`` on the |
| 255 | +``price`` field as grouped by the ``cust_id`` field. |
| 256 | + |
| 257 | +Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and |
| 258 | +``price`` as they appear as operands to the aggregation operators. |
| 259 | + |
| 260 | +WHERE ... GROUP BY ... HAVING ... |
| 261 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 262 | + |
| 263 | +MongoDB provides aggregation :agg:expression:`$group` and |
| 264 | +:agg:expression:`$match` operators to perform the SQL operation of |
| 265 | +``WHERE ... GROUP BY ... HAVIN``. |
| 266 | + |
| 267 | +Consider the following SQL command which can be used to find the |
| 268 | +``cust_id`` and the calculated column ``total`` for orders with status |
| 269 | +``A`` grouped by ``cust_id`` having ``total`` greater than ``250``: |
| 270 | + |
| 271 | +.. code-block:: sql |
| 272 | + |
| 273 | + SELECT cust_id, SUM(price) as total |
| 274 | + FROM orders |
| 275 | + WHERE status = 'A' |
| 276 | + GROUP BY cust_id |
| 277 | + HAVING total > 250 |
| 278 | + |
| 279 | +In MongoDB, the analogous query using the |
| 280 | +:doc:`/applications/aggregation` is: |
| 281 | + |
| 282 | +.. code-block:: javascript |
| 283 | + :emphasize-lines: 2-4 |
| 284 | + |
| 285 | + db.orders.aggregate( [ |
| 286 | + { $match: { status: 'A' } }, |
| 287 | + { $group: { _id: "$cust_id", total: { $sum: "$price" } } } , |
| 288 | + { $match: { total: { $gt: 250 } } } |
| 289 | + ] ) |
| 290 | + |
| 291 | +The :doc:`/applications/aggregation` query first performs a |
| 292 | +:agg:expression:`$match` operation that returns only those documents |
| 293 | +that have ``status`` field equal to ``A``. Then the query pipes the |
| 294 | +results to the :agg:pipeline:`$group` operation that uses the |
| 295 | +:agg:expression:`$sum` operator to calculate the ``total`` on the |
| 296 | +``price`` field as grouped by the ``cust_id`` field. Finally the query |
| 297 | +pipes those results to the :agg:expression:`$match` operator that |
| 298 | +returns only those results that have ``total`` greater than ``250``. |
| 299 | + |
| 300 | +Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and |
| 301 | +``price`` as they appear as operands to the aggregation operators. |
0 commit comments