Skip to content

Commit 568f468

Browse files
committed
DOCS-53 aggregation framework to SQL mapping
1 parent 673c95f commit 568f468

File tree

6 files changed

+310
-6
lines changed

6 files changed

+310
-6
lines changed

bin/makefile.tables

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
.PHONY: tables
55

66
# a phony target to build all table pages, add each built table to this dependency here:
7-
tables: # source/includes/table-sql-to-mongo.rst
7+
tables: source/includes/table-sql-to-agg-examples.rst source/includes/table-sql-to-agg-terms.rst
88
# a noop until there are actual tables here
99

1010
# each table will build from a '.yaml' table file the includes
1111
# directory, into a '.rst' file in the includes directory, using the
1212
# 'table_builder.py' script. The build rules for these files will look
1313
# like the following.
1414

15-
# source/includes/table-sql-to-mongo.rst:source/includes/table/sql-to-mongo.yaml
16-
# @$(PYTHONBIN) bin/table_builder.py $< $@
17-
# @[table-builder]: \(re\)generate $@ table for inclusion in build
15+
source/includes/table-sql-to-agg-terms.rst:source/includes/table/sql-to-agg-terms.yaml
16+
@$(PYTHONBIN) bin/table_builder.py $< $@
17+
@[table-builder]: \(re\)generate $@ table for inclusion in build
18+
source/includes/table-sql-to-agg-examples.rst:source/includes/table/sql-to-agg-examples.yaml
19+
@$(PYTHONBIN) bin/table_builder.py $< $@
20+
@[table-builder]: \(re\)generate $@ table for inclusion in build
21+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.. default-domain:: mongodb
2+
3+
SQL to Aggregation Framework
4+
----------------------------
5+
6+
With its :doc:`aggregation framework </applications/aggregation>`,
7+
MongoDB provides analogous functionality to common SQL aggregation
8+
functions.
9+
10+
The following table presents a quick reference of the various SQL
11+
aggregation terms, functions, and concepts and the corresponding
12+
MongoDB :ref:`aggregation operators
13+
<aggregation-pipeline-operator-reference>`:
14+
15+
.. include:: /includes/table-sql-to-agg-terms.rst
16+
17+
Examples
18+
~~~~~~~~
19+
20+
The following table presents a quick reference of SQL aggregation
21+
statements and the corresponding MongoDB statements. The examples in
22+
the table assume the following conditions:
23+
24+
- The SQL examples assume *two* tables, ``orders`` and
25+
``order_lineitem`` that join by the ``order_lineitem.order_id`` and
26+
the ``orders.id`` columns.
27+
28+
- The MongoDB examples assume *one* collection ``orders`` that contain
29+
documents of the following prototype:
30+
31+
.. code-block:: javascript
32+
33+
{
34+
cust_id: "abc123",
35+
ord_date: ISODate("2012-11-02T17:04:11.102Z"),
36+
status: 'A',
37+
price: 50,
38+
items: [ { sku: "xxx", qty: 25, price: 1 },
39+
{ sku: "yyy", qty: 25, price: 1 } ]
40+
}
41+
42+
- In the MongoDB statements, the fields of the :term:`documents
43+
<document>` in the collection ``orders`` are prefixed with ``$`` when
44+
they appear as operands to the aggregation operators.
45+
46+
.. include:: /includes/table-sql-to-agg-examples.rst
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# table structure. all content symbolic.
2+
section: layout
3+
header: [ meta.header1, meta.header2 ]
4+
rows:
5+
- 1: [ content.sql1, content.mongo1 ]
6+
- 2: [ content.sql2, content.mongo2 ]
7+
- 3: [ content.sql3, content.mongo3 ]
8+
- 4: [ content.sql4, content.mongo4 ]
9+
- 5: [ content.sql5, content.mongo5 ]
10+
- 6: [ content.sql6, content.mongo6 ]
11+
- 7: [ content.sql7, content.mongo7 ]
12+
- 8: [ content.sql8, content.mongo8 ]
13+
- 9: [ content.sql9, content.mongo9 ]
14+
---
15+
# table metadata, as meta.<key>
16+
section: meta
17+
header1: "SQL Example"
18+
header2: "MongoDB Example"
19+
---
20+
# table content, as content.<key>
21+
section: content
22+
sql1: |
23+
.. code-block:: sql
24+
25+
SELECT COUNT(*) AS count
26+
FROM orders
27+
mongo1: |
28+
.. code-block:: javascript
29+
:emphasize-lines: 2
30+
31+
db.orders.aggregate( [
32+
{ $group: { _id: null, count: { $sum: 1 } } }
33+
] )
34+
sql2: |
35+
.. code-block:: sql
36+
37+
SELECT SUM(price) AS total
38+
FROM orders
39+
mongo2: |
40+
.. code-block:: javascript
41+
:emphasize-lines: 2
42+
43+
db.orders.aggregate( [
44+
{ $group: { _id: null, total: { $sum: "$price" } } }
45+
] )
46+
sql3: |
47+
.. code-block:: sql
48+
49+
SELECT cust_id, SUM(price) AS total
50+
FROM orders
51+
GROUP BY cust_id
52+
mongo3: |
53+
54+
.. code-block:: javascript
55+
:emphasize-lines: 2
56+
57+
db.orders.aggregate( [
58+
{ $group: { _id: "$cust_id", total: { $sum: "$price" } } }
59+
] )
60+
sql4: |
61+
.. code-block:: sql
62+
63+
SELECT cust_id, ord_date, SUM(price) AS total
64+
FROM orders
65+
GROUP BY cust_id, ord_date
66+
mongo4: |
67+
.. code-block:: javascript
68+
:emphasize-lines: 2-3
69+
70+
db.orders.aggregate( [
71+
{ $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" },
72+
total: { $sum: "$price" } } }
73+
] )
74+
sql5: |
75+
.. code-block:: sql
76+
77+
SELECT cust_id, count(*)
78+
FROM orders
79+
GROUP BY cust_id
80+
HAVING count(*) > 1
81+
mongo5: |
82+
.. code-block:: javascript
83+
:emphasize-lines: 2-3
84+
85+
db.orders.aggregate( [
86+
{ $group: { _id: "$cust_id", count: { $sum: 1 } } },
87+
{ $match: { count: { $gt: 1 } } }
88+
] )
89+
sql6: |
90+
.. code-block:: sql
91+
92+
SELECT cust_id, ord_date, SUM(price) AS total
93+
FROM orders
94+
GROUP BY cust_id, ord_date
95+
HAVING total > 250
96+
mongo6: |
97+
.. code-block:: javascript
98+
:emphasize-lines: 2-4
99+
100+
db.orders.aggregate( [
101+
{ $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" },
102+
total: { $sum: "$price" } } },
103+
{ $match: { total: { $gt: 250 } } }
104+
] )
105+
sql7: |
106+
.. code-block:: sql
107+
108+
SELECT cust_id, SUM(price) as total
109+
FROM orders
110+
WHERE status = 'A'
111+
GROUP BY cust_id
112+
mongo7: |
113+
.. code-block:: javascript
114+
:emphasize-lines: 2-3
115+
116+
db.orders.aggregate( [
117+
{ $match: { status: 'A' } },
118+
{ $group: { _id: "$cust_id", total: { $sum: "$price" } } }
119+
] )
120+
sql8: |
121+
.. code-block:: sql
122+
123+
SELECT cust_id, SUM(price) as total
124+
FROM orders
125+
WHERE status = 'A'
126+
GROUP BY cust_id
127+
HAVING total > 250
128+
mongo8: |
129+
.. code-block:: javascript
130+
:emphasize-lines: 2-5
131+
132+
db.orders.aggregate( [
133+
{ $match: { status: 'A' } },
134+
{ $group: { _id: "$cust_id", total: { $sum: "$price" } } } ,
135+
{ $match: { total: { $gt: 250 } } }
136+
] )
137+
sql9: |
138+
.. code-block:: sql
139+
140+
SELECT cust_id,sum(li.qty) as qty
141+
FROM orders o, order_lineitem li
142+
WHERE li.order_id=o.id
143+
GROUP BY cust_id
144+
mongo9: |
145+
.. code-block:: javascript
146+
:emphasize-lines: 2-5
147+
148+
db.orders.aggregate( [
149+
{ $unwind: "$items" },
150+
{ $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } }
151+
] )
152+
...
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# table structure. all content symbolic.
2+
section: layout
3+
header: [ meta.header1, meta.header2 ]
4+
rows:
5+
- 1: [ content.sql1, content.mongo1 ]
6+
- 2: [ content.sql2, content.mongo2 ]
7+
- 3: [ content.sql3, content.mongo3 ]
8+
- 4: [ content.sql4, content.mongo4 ]
9+
- 5: [ content.sql5, content.mongo5 ]
10+
- 6: [ content.sql6, content.mongo6 ]
11+
- 7: [ content.sql7, content.mongo7 ]
12+
- 8: [ content.sql8, content.mongo8 ]
13+
- 9: [ content.sql9, content.mongo9 ]
14+
---
15+
# table metadata, as meta.<key>
16+
section: meta
17+
header1: "SQL Terms, Functions, and Concepts"
18+
header2: "MongoDB Aggregation Operators"
19+
---
20+
# table content, as content.<key>
21+
section: content
22+
sql1: WHERE
23+
mongo1: :agg:pipeline:`$match`
24+
sql2: GROUP BY
25+
mongo2: :agg:pipeline:`$group`
26+
sql3: HAVING
27+
mongo3: :agg:pipeline:`$match`
28+
sql4: SELECT
29+
mongo4: :agg:pipeline:`$project`
30+
sql5: ORDER BY
31+
mongo5: :agg:pipeline:`$sort`
32+
sql6: LIMIT
33+
mongo6: :agg:pipeline:`$limit`
34+
sql7: SUM()
35+
mongo7: :agg:pipeline:`$sum`
36+
sql8: COUNT()
37+
mongo8: :agg:pipeline:`$sum`
38+
sql9: join
39+
mongo9: |
40+
No direct corresponding operator;
41+
*however*, the :agg:pipeline:`$unwind` operator
42+
allows for somewhat similar functionality,
43+
but with fields embedded within the document.
44+
...

themes/epub_mongodb/static/epub.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,35 @@ div.highlight-javascript>div.highlight>pre>span.hll>span.nx {
392392
font-weight: bold;
393393
}
394394

395+
/* Specific to the SQL to Aggregation Framework page -- Begin */
396+
397+
div#sql-to-aggregation-framework td pre {
398+
border: none;
399+
-webkit-box-shadow: none;
400+
-moz-box-shadow: none;
401+
margin: 0px;
402+
padding: 5px;
403+
background-color: transparent;
404+
}
405+
406+
div#sql-to-aggregation-framework table.docutils {
407+
border-collapse:collapse;
408+
border:1px solid black;
409+
}
410+
411+
div#sql-to-aggregation-framework table.docutils>colgroup>col {
412+
border-collapse:collapse;
413+
border-left:1px solid black;
414+
}
415+
416+
div#sql-to-aggregation-framework table.docutils>thead th.head{
417+
padding-top: 5px;
418+
padding-bottom: 5px;
419+
background-color: #F7F2E0;
420+
}
421+
422+
/* Specific to the SQL to Aggregation Framework page -- End */
423+
395424
/* -- math display ---------------------------------------------------------- */
396425

397426
img.math {

themes/mongodb/static/mongodb-docs.css_t

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,42 @@ table.docutils.field-list ul.first.last.simple>li>p {
312312
padding-top: 1em;
313313
}
314314

315-
div.highlight-javascript>div.highlight>pre>span.hll {
315+
div.first.last.highlight-javascript>div.highlight>pre>span.hll {
316316
background-color: transparent;
317317
}
318-
div.highlight-javascript>div.highlight>pre>span.hll>span.nx {
318+
div.first.last.highlight-javascript>div.highlight>pre>span.hll>span.nx {
319319
font-weight: bold;
320320
}
321321

322+
/* Specific to the SQL to Aggregation Framework page -- Begin */
323+
324+
div#sql-to-aggregation-framework td pre {
325+
border: none;
326+
-webkit-box-shadow: none;
327+
-moz-box-shadow: none;
328+
margin: 0px;
329+
padding: 5px;
330+
background-color: transparent;
331+
}
332+
333+
div#sql-to-aggregation-framework table.docutils {
334+
border-collapse:collapse;
335+
border:1px solid black;
336+
}
337+
338+
div#sql-to-aggregation-framework table.docutils>colgroup>col {
339+
border-collapse:collapse;
340+
border-left:1px solid black;
341+
}
342+
343+
div#sql-to-aggregation-framework table.docutils>thead th.head{
344+
padding-top: 5px;
345+
padding-bottom: 5px;
346+
background-color: #F7F2E0;
347+
}
348+
349+
/* Specific to the SQL to Aggregation Framework page -- End */
350+
322351
/* for main page to knock out the bullets & padding for main columns */
323352

324353
div#mongodb ul{

0 commit comments

Comments
 (0)