Skip to content

Commit 5327b3e

Browse files
committed
DOCS-53 added sort example
1 parent 231de31 commit 5327b3e

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

source/sql-reference/sql-aggregation.txt

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,15 @@ results to the :agg:pipeline:`$group` operation that uses the
254254
:agg:expression:`$sum` operator to calculate the ``total`` on the
255255
``price`` field as grouped by the ``cust_id`` field.
256256

257-
Note the ``$`` in front of the fields ``cust_id``, ``ord_date``, and
257+
Note the ``$`` in front of the fields ``cust_id`` and
258258
``price`` as they appear as operands to the aggregation operators.
259259

260260
WHERE ... GROUP BY ... HAVING ...
261261
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262262

263263
MongoDB provides aggregation :agg:expression:`$group` and
264264
:agg:expression:`$match` operators to perform the SQL operation of
265-
``WHERE ... GROUP BY ... HAVIN``.
265+
``WHERE ... GROUP BY ... HAVING``.
266266

267267
Consider the following SQL command which can be used to find the
268268
``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
297297
pipes those results to the :agg:expression:`$match` operator that
298298
returns only those results that have ``total`` greater than ``250``.
299299

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
301351
``price`` as they appear as operands to the aggregation operators.

0 commit comments

Comments
 (0)