对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

SQL 聚合映射图表

聚合管道映射到许多常见的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 示例假设有两个表 orders 和 order_lineitem,它们通过 order_lineitem.order_id 和 orders.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 countFROM orders
db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } }] )

计算以下位置所有记录的数量: orders

SELECT SUM(price) AS totalFROM orders
db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } }] )

对 orders 中的 price 字段求和

SELECT cust_id, SUM(price) AS totalFROM ordersGROUP BY cust_id
db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] )

对于每个唯一的 cust_id,对 price 字段求和。

SELECT cust_id, SUM(price) AS totalFROM ordersGROUP BY cust_idORDER 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 totalFROM ordersGROUP 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 ordersGROUP BY cust_idHAVING 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 totalFROM ordersGROUP BY cust_id, ord_dateHAVING 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 totalFROM ordersWHERE 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 totalFROM ordersWHERE status = 'A'GROUP BY cust_idHAVING 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 qtyFROM orders o, order_lineitem liWHERE li.order_id = o.idGROUP 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_id、ord_date 分组的数量。不包括日期的时间部分。

给本页内容打分

在此页面上