文档菜单
文档首页
/
MongoDB 手册
/ /

SQL 到聚合映射图

本页内容

  • 示例

聚合管道允许 MongoDB 提供原生聚合能力,这些能力对应于许多常见的 SQL 数据聚合操作。

下表概述了常见的 SQL 聚合术语、函数和概念及其对应的 MongoDB 聚合运算符:

SQL 术语、函数和概念
MongoDB 聚合运算符
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
LIMIT
SUM()
COUNT()
join
SELECT INTO NEW_TABLE
MERGE INTO TABLE
UNION ALL

有关所有聚合管道和表达式运算符的列表,请参阅

提示

另请参阅

以下表格展示了 SQL 聚合语句及其对应的 MongoDB 语句的快速参考。表中的示例假设以下条件

  • SQL 示例假设有两个表,分别是 ordersorder_lineitem,它们通过 order_lineitem.order_idorders.id 列进行连接。

  • MongoDB 示例假设有一个名为 orders 的集合,其中包含以下原型文档

    {
    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 } ]
    }
SQL 示例
MongoDB 示例
描述
SELECT COUNT(*) AS count
FROM orders
db.orders.aggregate( [
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
] )
orders 计数所有记录
SELECT SUM(price) AS total
FROM orders
db.orders.aggregate( [
{
$group: {
_id: null,
total: { $sum: "$price" }
}
}
] )
orders 计算字段 price 的总和
SELECT cust_id,
SUM(price) AS total
FROM orders
GROUP BY cust_id
db.orders.aggregate( [
{
$group: {
_id: "$cust_id",
total: { $sum: "$price" }
}
}
] )
对于每个唯一的 cust_id,计算字段 price 的总和。
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 } }
] )
对于每个唯一的 cust_id,计算字段 price 的总和,并按总和排序。
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: { $dateToString: {
format: "%Y-%m-%d",
date: "$ord_date"
}}
},
total: { $sum: "$price" }
}
}
] )
对于每个唯一的 cust_id,按 ord_date 分组,计算字段 price 的总和。排除日期的时间部分。
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 } } }
] )
对于具有多个记录的 cust_id,返回 cust_id 和相应的记录计数。
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: { $dateToString: {
format: "%Y-%m-%d",
date: "$ord_date"
}}
},
total: { $sum: "$price" }
}
},
{ $match: { total: { $gt: 250 } } }
] )
对于每个唯一的 cust_id,按 ord_date 分组,计算字段 price 的总和,并仅返回总和大于 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" }
}
}
] )
对于状态为 A 的每个唯一的 cust_id,计算字段 price 的总和。
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 } } }
] )
对于状态为 A 的每个唯一的 cust_id,计算字段 price 的总和,并仅返回总和大于 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" }
}
}
] )
对于每个唯一的 cust_id,计算与订单关联的相应行项目 qty 字段的总和。
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: { $dateToString: {
format: "%Y-%m-%d",
date: "$ord_date"
}}
}
}
},
{
$group: {
_id: null,
count: { $sum: 1 }
}
}
] )
计数具有不同的 cust_idord_date 分组的数量。排除日期的时间部分。

返回

变量

本页内容