Skip to content

DOCS-53 aggregation framework to SQL mapping #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions bin/makefile.tables
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
.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
# directory, into a '.rst' file in the includes directory, using the
# '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

46 changes: 46 additions & 0 deletions draft/reference/sql-reference/sql-aggregation-xref.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
.. default-domain:: mongodb

SQL to Aggregation Framework
----------------------------

With its :doc:`aggregation framework </applications/aggregation>`,
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
<aggregation-pipeline-operator-reference>`:

.. 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
<document>` in the collection ``orders`` are prefixed with ``$`` when
they appear as operands to the aggregation operators.

.. include:: /includes/table-sql-to-agg-examples.rst
152 changes: 152 additions & 0 deletions source/includes/table/sql-to-agg-examples.yaml
Original file line number Diff line number Diff line change
@@ -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.<key>
section: meta
header1: "SQL Example"
header2: "MongoDB Example"
---
# table content, as content.<key>
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" } } }
] )
...
44 changes: 44 additions & 0 deletions source/includes/table/sql-to-agg-terms.yaml
Original file line number Diff line number Diff line change
@@ -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.<key>
section: meta
header1: "SQL Terms, Functions, and Concepts"
header2: "MongoDB Aggregation Operators"
---
# table content, as content.<key>
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.
...
29 changes: 29 additions & 0 deletions themes/epub_mongodb/static/epub.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 31 additions & 2 deletions themes/mongodb/static/mongodb-docs.css_t
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down