AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

SQL から集計へのマッピングチャート

集計パイプラインは、多くの一般的なSQL集計操作にマップされます。

次の表は、一般的なSQL集計用語、関数、概念と、それに対応するMongoDB集計演算子を比較したものです。

SQLの用語、関数、および概念
MongoDB 集計演算子

WHERE

グループ化基準:

HAVING

SELECT

並び替え基準:

LIMIT

SUM()

COUNT()

join

SELECT INTO NEW_TABLE

MERGE INTO TABLE

UNION ALL

すべての集計パイプラインおよび式演算子のリストについては、以下を参照してください。

次の表は、SQL 集計ステートメントと対応する MongoDB ステートメントのクイック リファレンスを示しています。この表の例は、次の条件を前提としています。

  • SQL の例では、order_lineitem.order_id 列と orders.id 列で結合される 2 つの表 orders と order_lineitem を想定しています。

  • MongoDB の例では、次のプロトタイプのドキュメントを含む 1 つのコレクション 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 グループの数をカウントします。日付の時刻部分を除外します。

このページを評価

項目一覧