SQL to Aggregation Framework Mapping Chart

本文介绍如何将常见的SQL聚合操作映射到MongoDB的聚合框架中,包括WHERE、GROUP BY、HAVING等SQL概念对应的MongoDB操作符,并通过多个实例展示了具体的转换过程。
 

SQL to Aggregation Framework Mapping Chart

The aggregation framework allows MongoDB to provide native aggregation capabilities that corresponds to many common data aggregation operations in SQL. If you’re new to MongoDB you might want to consider the Frequently Asked Questions section for a selection of common questions.

The following table provides an overview of common SQL aggregation terms, functions, and concepts and the corresponding MongoDBaggregation operators:

SQL Terms, Functions, and ConceptsMongoDB Aggregation Operators
WHERE$match
GROUP BY$group
HAVING$match
SELECT$project
ORDER BY$sort
LIMIT$limit
SUM()$sum
COUNT()$sum
joinNo direct corresponding operator;however, the $unwind operator allows for somewhat similar functionality, but with fields embedded within the document.

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 andorder_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:

    {
      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 } ]
    }
    
  • The MongoDB statements prefix the names of the fields from thedocuments in the collection orders with a$ character when they appear as operands to the aggregation operations.

SQL ExampleMongoDB ExampleDescription
SELECT COUNT(*) AS count
FROM orders
db.orders.aggregate( [
   { $group: { _id: null,
               count: { $sum: 1 } } }
] )
Count all records from orders
SELECT SUM(price) AS total
FROM orders
db.orders.aggregate( [
   { $group: { _id: null,
               total: { $sum: "$price" } } }
] )
Sum the price field from orders
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
db.orders.aggregate( [
   { $group: { _id: "$cust_id",
               total: { $sum: "$price" } } }
] )
For each unique cust_id, sum the price field.
SELECT cust_id,
       SUM(price) AS total
FROM orders
GROUP BY cust_id
ORDER BY total
db.orders.aggregate( [
   { $group: { _id: "$cust_id",
               total: { $sum: "$price" } } },
   { $sort: { total: 1 } }
] )
For each unique cust_id, sum the price field, results sorted by sum.
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id, ord_date
db.orders.aggregate( [
   { $group: { _id: { cust_id: "$cust_id",
                      ord_date: "$ord_date" },
               total: { $sum: "$price" } } }
] )
For each uniquecust_id, ord_date grouping, sum the price field.
SELECT cust_id, count(*)
FROM orders
GROUP BY cust_id
HAVING count(*) > 1
db.orders.aggregate( [
   { $group: { _id: "$cust_id",
               count: { $sum: 1 } } },
   { $match: { count: { $gt: 1 } } }
] )
For cust_id with multiple records, return the cust_id and the corresponding record count.
SELECT cust_id,
       ord_date,
       SUM(price) AS total
FROM orders
GROUP BY cust_id, ord_date
HAVING total > 250
db.orders.aggregate( [
   { $group: { _id: { cust_id: "$cust_id",
                      ord_date: "$ord_date" },
               total: { $sum: "$price" } } },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_id, ord_dategrouping, sum the price field and return only where the sum is greater than 250.
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
db.orders.aggregate( [
   { $match: { status: 'A' } },
   { $group: { _id: "$cust_id",
               total: { $sum: "$price" } } }
] )
For each unique cust_idwith status A, sum the price field.
SELECT cust_id,
       SUM(price) as total
FROM orders
WHERE status = 'A'
GROUP BY cust_id
HAVING total > 250
db.orders.aggregate( [
   { $match: { status: 'A' } },
   { $group: { _id: "$cust_id",
               total: { $sum: "$price" } } },
   { $match: { total: { $gt: 250 } } }
] )
For each unique cust_idwith status A, sum the price field and return only where the sum is greater than 250.
SELECT cust_id,
       SUM(li.qty) as qty
FROM orders o,
     order_lineitem li
WHERE li.order_id = o.id
GROUP BY cust_id
db.orders.aggregate( [
   { $unwind: "$items" },
   { $group: { _id: "$cust_id",
               qty: { $sum: "$items.qty" } } }
] )
For each unique cust_id, sum the corresponding line item qty fields associated with the orders.
SELECT COUNT(*)
FROM (SELECT cust_id, ord_date
      FROM orders
      GROUP BY cust_id, ord_date) as DerivedTable
db.orders.aggregate( [
   { $group: { _id: { cust_id: "$cust_id",
                      ord_date: "$ord_date" } } },
   { $group: { _id: null, count: { $sum: 1 } } }
] )

转载于:https://www.cnblogs.com/fx2008/archive/2013/03/28/2986592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值