@@ -254,15 +254,15 @@ results to the :agg:pipeline:`$group` operation that uses the
254
254
:agg:expression:`$sum` operator to calculate the ``total`` on the
255
255
``price`` field as grouped by the ``cust_id`` field.
256
256
257
- Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and
257
+ Note the ``$`` in front of the fields ``cust_id`` and
258
258
``price`` as they appear as operands to the aggregation operators.
259
259
260
260
WHERE ... GROUP BY ... HAVING ...
261
261
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262
262
263
263
MongoDB provides aggregation :agg:expression:`$group` and
264
264
:agg:expression:`$match` operators to perform the SQL operation of
265
- ``WHERE ... GROUP BY ... HAVIN ``.
265
+ ``WHERE ... GROUP BY ... HAVING ``.
266
266
267
267
Consider the following SQL command which can be used to find the
268
268
``cust_id`` and the calculated column ``total`` for orders with status
@@ -297,5 +297,55 @@ results to the :agg:pipeline:`$group` operation that uses the
297
297
pipes those results to the :agg:expression:`$match` operator that
298
298
returns only those results that have ``total`` greater than ``250``.
299
299
300
- Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and
300
+ Note the ``$`` in front of the fields ``cust_id`` and
301
+ ``price`` as they appear as operands to the aggregation operators.
302
+
303
+ WHERE ... GROUP BY ... HAVING ... ORDER BY
304
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
305
+
306
+ MongoDB provides aggregation :agg:expression:`$group` and
307
+ :agg:expression:`$match` operators to perform the SQL operation of
308
+ ``WHERE ... GROUP BY ... HAVING ... ORDER BY``.
309
+
310
+ Consider the following SQL command which can be used to find the
311
+ ``cust_id`` and the calculated column ``total`` for orders with status
312
+ ``A`` grouped by ``cust_id`` having ``total`` greater than ``250``. The
313
+ results will be sorted first by descending ``total`` and then by
314
+ ascending ``cust_id``:
315
+
316
+ .. code-block:: sql
317
+
318
+ SELECT cust_id, SUM(price) as total
319
+ FROM orders
320
+ WHERE status = 'A'
321
+ GROUP BY cust_id
322
+ HAVING total > 250
323
+ ORDER BY total DESC, cust_id ASC
324
+
325
+ In MongoDB, the analogous query using the
326
+ :doc:`/applications/aggregation` is:
327
+
328
+ .. code-block:: javascript
329
+ :emphasize-lines: 2-5
330
+
331
+ db.orders.aggregate( [
332
+ { $match: { status: 'A' } },
333
+ { $group: { _id: "$cust_id", total: { $sum: "$price" } } } ,
334
+ { $match: { total: { $gt: 250 } } },
335
+ { $sort: { total: -1, _id: 1 } }
336
+ ] )
337
+
338
+ The :doc:`/applications/aggregation` query first performs a
339
+ :agg:expression:`$match` operation that returns only those documents
340
+ that have ``status`` field equal to ``A``. Next, the query pipes the
341
+ results to the :agg:pipeline:`$group` operation that uses the
342
+ :agg:expression:`$sum` operator to calculate the ``total`` on the
343
+ ``price`` field as grouped by the ``cust_id`` field. Then, the query
344
+ pipes those results to the :agg:expression:`$match` operator that
345
+ returns only those results that have ``total`` greater than ``250``.
346
+ Finally, the query pipes those results to the :agg:expression:`$sort`
347
+ operator that will first sort by ``total`` descending and then by
348
+ ``_id`` ascending.
349
+
350
+ Note the ``$`` in front of the fields ``cust_id`` and
301
351
``price`` as they appear as operands to the aggregation operators.
0 commit comments