diff --git a/bin/makefile.tables b/bin/makefile.tables index 5fd7fd45fd9..46db5f15412 100644 --- a/bin/makefile.tables +++ b/bin/makefile.tables @@ -4,7 +4,7 @@ .PHONY: tables # a phony target to build all table pages, add each built table to this dependency here: -tables: # source/includes/table-sql-to-mongo.rst +tables: source/includes/table-sql-to-agg-examples.rst source/includes/table-sql-to-agg-terms.rst # a noop until there are actual tables here # each table will build from a '.yaml' table file the includes @@ -12,6 +12,10 @@ tables: # source/includes/table-sql-to-mongo.rst # 'table_builder.py' script. The build rules for these files will look # like the following. -# source/includes/table-sql-to-mongo.rst:source/includes/table/sql-to-mongo.yaml -# @$(PYTHONBIN) bin/table_builder.py $< $@ -# @[table-builder]: \(re\)generate $@ table for inclusion in build +source/includes/table-sql-to-agg-terms.rst:source/includes/table/sql-to-agg-terms.yaml + @$(PYTHONBIN) bin/table_builder.py $< $@ + @[table-builder]: \(re\)generate $@ table for inclusion in build +source/includes/table-sql-to-agg-examples.rst:source/includes/table/sql-to-agg-examples.yaml + @$(PYTHONBIN) bin/table_builder.py $< $@ + @[table-builder]: \(re\)generate $@ table for inclusion in build + diff --git a/draft/reference/sql-reference/sql-aggregation-xref.txt b/draft/reference/sql-reference/sql-aggregation-xref.txt new file mode 100644 index 00000000000..6b69adc7c05 --- /dev/null +++ b/draft/reference/sql-reference/sql-aggregation-xref.txt @@ -0,0 +1,46 @@ +.. default-domain:: mongodb + +SQL to Aggregation Framework +---------------------------- + +With its :doc:`aggregation framework `, +MongoDB provides analogous functionality to common SQL aggregation +functions. + +The following table presents a quick reference of the various SQL +aggregation terms, functions, and concepts and the corresponding +MongoDB :ref:`aggregation operators +`: + +.. include:: /includes/table-sql-to-agg-terms.rst + +Examples +~~~~~~~~ + +The following table presents a quick reference of SQL aggregation +statements and the corresponding MongoDB statements. The examples in +the table assume the following conditions: + +- The SQL examples assume *two* tables, ``orders`` and + ``order_lineitem`` that join by the ``order_lineitem.order_id`` and + the ``orders.id`` columns. + +- The MongoDB examples assume *one* collection ``orders`` that contain + documents of the following prototype: + + .. code-block:: javascript + + { + cust_id: "abc123", + ord_date: ISODate("2012-11-02T17:04:11.102Z"), + status: 'A', + price: 50, + items: [ { sku: "xxx", qty: 25, price: 1 }, + { sku: "yyy", qty: 25, price: 1 } ] + } + +- In the MongoDB statements, the fields of the :term:`documents + ` in the collection ``orders`` are prefixed with ``$`` when + they appear as operands to the aggregation operators. + +.. include:: /includes/table-sql-to-agg-examples.rst diff --git a/source/includes/table/sql-to-agg-examples.yaml b/source/includes/table/sql-to-agg-examples.yaml new file mode 100644 index 00000000000..4b5fb720db3 --- /dev/null +++ b/source/includes/table/sql-to-agg-examples.yaml @@ -0,0 +1,152 @@ +# table structure. all content symbolic. +section: layout +header: [ meta.header1, meta.header2 ] +rows: + - 1: [ content.sql1, content.mongo1 ] + - 2: [ content.sql2, content.mongo2 ] + - 3: [ content.sql3, content.mongo3 ] + - 4: [ content.sql4, content.mongo4 ] + - 5: [ content.sql5, content.mongo5 ] + - 6: [ content.sql6, content.mongo6 ] + - 7: [ content.sql7, content.mongo7 ] + - 8: [ content.sql8, content.mongo8 ] + - 9: [ content.sql9, content.mongo9 ] +--- +# table metadata, as meta. +section: meta +header1: "SQL Example" +header2: "MongoDB Example" +--- +# table content, as content. +section: content +sql1: | + .. code-block:: sql + + SELECT COUNT(*) AS count + FROM orders +mongo1: | + .. code-block:: javascript + :emphasize-lines: 2 + + db.orders.aggregate( [ + { $group: { _id: null, count: { $sum: 1 } } } + ] ) +sql2: | + .. code-block:: sql + + SELECT SUM(price) AS total + FROM orders +mongo2: | + .. code-block:: javascript + :emphasize-lines: 2 + + db.orders.aggregate( [ + { $group: { _id: null, total: { $sum: "$price" } } } + ] ) +sql3: | + .. code-block:: sql + + SELECT cust_id, SUM(price) AS total + FROM orders + GROUP BY cust_id +mongo3: | + + .. code-block:: javascript + :emphasize-lines: 2 + + db.orders.aggregate( [ + { $group: { _id: "$cust_id", total: { $sum: "$price" } } } + ] ) +sql4: | + .. code-block:: sql + + SELECT cust_id, ord_date, SUM(price) AS total + FROM orders + GROUP BY cust_id, ord_date +mongo4: | + .. code-block:: javascript + :emphasize-lines: 2-3 + + db.orders.aggregate( [ + { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, + total: { $sum: "$price" } } } + ] ) +sql5: | + .. code-block:: sql + + SELECT cust_id, count(*) + FROM orders + GROUP BY cust_id + HAVING count(*) > 1 +mongo5: | + .. code-block:: javascript + :emphasize-lines: 2-3 + + db.orders.aggregate( [ + { $group: { _id: "$cust_id", count: { $sum: 1 } } }, + { $match: { count: { $gt: 1 } } } + ] ) +sql6: | + .. code-block:: sql + + SELECT cust_id, ord_date, SUM(price) AS total + FROM orders + GROUP BY cust_id, ord_date + HAVING total > 250 +mongo6: | + .. code-block:: javascript + :emphasize-lines: 2-4 + + db.orders.aggregate( [ + { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, + total: { $sum: "$price" } } }, + { $match: { total: { $gt: 250 } } } + ] ) +sql7: | + .. code-block:: sql + + SELECT cust_id, SUM(price) as total + FROM orders + WHERE status = 'A' + GROUP BY cust_id +mongo7: | + .. code-block:: javascript + :emphasize-lines: 2-3 + + db.orders.aggregate( [ + { $match: { status: 'A' } }, + { $group: { _id: "$cust_id", total: { $sum: "$price" } } } + ] ) +sql8: | + .. code-block:: sql + + SELECT cust_id, SUM(price) as total + FROM orders + WHERE status = 'A' + GROUP BY cust_id + HAVING total > 250 +mongo8: | + .. code-block:: javascript + :emphasize-lines: 2-5 + + db.orders.aggregate( [ + { $match: { status: 'A' } }, + { $group: { _id: "$cust_id", total: { $sum: "$price" } } } , + { $match: { total: { $gt: 250 } } } + ] ) +sql9: | + .. code-block:: sql + + SELECT cust_id,sum(li.qty) as qty + FROM orders o, order_lineitem li + WHERE li.order_id=o.id + GROUP BY cust_id +mongo9: | + .. code-block:: javascript + :emphasize-lines: 2-5 + + db.orders.aggregate( [ + { $unwind: "$items" }, + { $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } } + ] ) +... diff --git a/source/includes/table/sql-to-agg-terms.yaml b/source/includes/table/sql-to-agg-terms.yaml new file mode 100644 index 00000000000..97ecd51042a --- /dev/null +++ b/source/includes/table/sql-to-agg-terms.yaml @@ -0,0 +1,44 @@ +# table structure. all content symbolic. +section: layout +header: [ meta.header1, meta.header2 ] +rows: + - 1: [ content.sql1, content.mongo1 ] + - 2: [ content.sql2, content.mongo2 ] + - 3: [ content.sql3, content.mongo3 ] + - 4: [ content.sql4, content.mongo4 ] + - 5: [ content.sql5, content.mongo5 ] + - 6: [ content.sql6, content.mongo6 ] + - 7: [ content.sql7, content.mongo7 ] + - 8: [ content.sql8, content.mongo8 ] + - 9: [ content.sql9, content.mongo9 ] +--- +# table metadata, as meta. +section: meta +header1: "SQL Terms, Functions, and Concepts" +header2: "MongoDB Aggregation Operators" +--- +# table content, as content. +section: content +sql1: WHERE +mongo1: :agg:pipeline:`$match` +sql2: GROUP BY +mongo2: :agg:pipeline:`$group` +sql3: HAVING +mongo3: :agg:pipeline:`$match` +sql4: SELECT +mongo4: :agg:pipeline:`$project` +sql5: ORDER BY +mongo5: :agg:pipeline:`$sort` +sql6: LIMIT +mongo6: :agg:pipeline:`$limit` +sql7: SUM() +mongo7: :agg:pipeline:`$sum` +sql8: COUNT() +mongo8: :agg:pipeline:`$sum` +sql9: join +mongo9: | + No direct corresponding operator; + *however*, the :agg:pipeline:`$unwind` operator + allows for somewhat similar functionality, + but with fields embedded within the document. +... \ No newline at end of file diff --git a/themes/epub_mongodb/static/epub.css b/themes/epub_mongodb/static/epub.css index 49f8b3eb859..2ae00b2e3dc 100644 --- a/themes/epub_mongodb/static/epub.css +++ b/themes/epub_mongodb/static/epub.css @@ -392,6 +392,35 @@ div.highlight-javascript>div.highlight>pre>span.hll>span.nx { font-weight: bold; } +/* Specific to the SQL to Aggregation Framework page -- Begin */ + +div#sql-to-aggregation-framework td pre { + border: none; + -webkit-box-shadow: none; + -moz-box-shadow: none; + margin: 0px; + padding: 5px; + background-color: transparent; +} + +div#sql-to-aggregation-framework table.docutils { + border-collapse:collapse; + border:1px solid black; +} + +div#sql-to-aggregation-framework table.docutils>colgroup>col { + border-collapse:collapse; + border-left:1px solid black; +} + +div#sql-to-aggregation-framework table.docutils>thead th.head{ + padding-top: 5px; + padding-bottom: 5px; + background-color: #F7F2E0; +} + +/* Specific to the SQL to Aggregation Framework page -- End */ + /* -- math display ---------------------------------------------------------- */ img.math { diff --git a/themes/mongodb/static/mongodb-docs.css_t b/themes/mongodb/static/mongodb-docs.css_t index 2cdcf757f47..6646a57f798 100644 --- a/themes/mongodb/static/mongodb-docs.css_t +++ b/themes/mongodb/static/mongodb-docs.css_t @@ -312,13 +312,42 @@ table.docutils.field-list ul.first.last.simple>li>p { padding-top: 1em; } -div.highlight-javascript>div.highlight>pre>span.hll { +div.first.last.highlight-javascript>div.highlight>pre>span.hll { background-color: transparent; } -div.highlight-javascript>div.highlight>pre>span.hll>span.nx { +div.first.last.highlight-javascript>div.highlight>pre>span.hll>span.nx { font-weight: bold; } +/* Specific to the SQL to Aggregation Framework page -- Begin */ + +div#sql-to-aggregation-framework td pre { + border: none; + -webkit-box-shadow: none; + -moz-box-shadow: none; + margin: 0px; + padding: 5px; + background-color: transparent; +} + +div#sql-to-aggregation-framework table.docutils { + border-collapse:collapse; + border:1px solid black; +} + +div#sql-to-aggregation-framework table.docutils>colgroup>col { + border-collapse:collapse; + border-left:1px solid black; +} + +div#sql-to-aggregation-framework table.docutils>thead th.head{ + padding-top: 5px; + padding-bottom: 5px; + background-color: #F7F2E0; +} + +/* Specific to the SQL to Aggregation Framework page -- End */ + /* for main page to knock out the bullets & padding for main columns */ div#mongodb ul{