SQL の用語、関数、および概念 |
MongoDB 集計演算子 |
どこ |
|
グループ化 |
|
持っている |
|
選択する |
|
注文方法 |
|
リミット |
|
和() |
|
カウント() |
|
加入 |
直接対応する演算子はありません。 ただし、 $unwind演算子を使用すると、ドキュメント内に埋め込まれたフィールドを使用して、同様の機能を使用できます。 |
[td]
SQLの例 |
MongoDB の例 |
説明 |
SELECT COUNT ( * ) AS countFROM 注文 |
db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } } ] ) |
注文からのすべてのレコードをカウントする |
注文から 合計としてSUM (価格) を選択 |
db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } } ] ) |
注文の価格フィールドを合計します。これは非常に便利です。公式の説明を読んでください。_ID が必要と書かれていますが、NULL になるとは予想していませんでした。 |
SELECT cust_id, SUM (価格) AS合計FROM 注文GROUP BY cust_id |
db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } } ] ) |
uniquecust_id ごとに、pricefield を合計します。 |
SELECT cust_id, SUM (価格) AS合計FROM 注文GROUP BY cust_id ORDER BY total |
db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }, { $sort: { total: 1 } } ] ) |
uniquecust_id ごとに、pricefield を合計し、結果を合計でソートします。 |
SELECT cust_id、ord_date、 SUM (価格) AS 合計FROM 注文GROUPBY cust_id、ord_date |
db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, total: { $sum: "$price" } } } ] ) |
uniquecust_id、ord_dategrouping ごとに、pricefield を合計します。 |
SELECT cust_id, count ( * ) FROM注文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 totalFROM ordersGROUPBY cust_id, ord_dateHAVING total > 250 |
db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" }, total: { $sum: "$price" } } }, { $match: { total: { $gt: 250 } } }] ) |
For each uniquecust_id,ord_dategrouping, sum the pricefield and return only where the sum is greater than 250. |
SELECT cust_id, SUM(price) astotalFROM ordersWHERE status ='A'GROUP BY cust_id |
db.orders.aggregate( [ { $match: { status: 'A' } }, { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] ) |
For each uniquecust_id with status A, sum the pricefield. |
SELECT cust_id, SUM(price) astotalFROM 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 } } }] ) |
For each uniquecust_id with status A, sum the pricefield and return only where the sum is greater than 250. |
SELECT cust_id, SUM(li.qty) asqtyFROM orders o, order_lineitem liWHERE li.order_id = o.idGROUP BYcust_id |
db.orders.aggregate( [ { $unwind: "$items" }, { $group: { _id: "$cust_id", qty: { $sum: "$items.qty" } } }] ) |
For each uniquecust_id, sum the corresponding line item qtyfields associated with the orders. |
SELECT COUNT(*)FROM (SELECT cust_id, ord_date FROM orders GROUP BYcust_id, ord_date) as DerivedTable |
db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" } } }, { $group: { _id: null, count: { $sum: 1 } } }] ) |