mongo与sql聚合操作对应图
2018-01-23 09:59
267 查看
SQL Terms, Functions, and Concepts | MongoDB Aggregation Operators |
WHERE | $match |
GROUP BY | $group |
HAVING | $match |
SELECT | $project |
ORDER BY | $sort |
LIMIT | $limit |
SUM() | $sum |
COUNT() | $sum |
join | No direct corresponding operator; however, the $unwindoperator allows for somewhat similar functionality, but with fields embedded within the document. |
SQL Example | MongoDB Example | Description |
SELECT COUNT(*) AScountFROM orders | db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } }] ) | Count all records fromorders |
SELECT SUM(price) AStotalFROM orders | db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } }] ) | Sum theprice field from orders,这个非常有用,看官方说明,说_ID是必须,但没想到可以为NULL, |
SELECT cust_id, SUM(price)AS totalFROM ordersGROUPBYcust_id | db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] ) | For each uniquecust_id, sum the pricefield. |
SELECT cust_id, SUM(price)AS totalFROM ordersGROUPBYcust_idORDERBY total | db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }, { $sort: { total: 1 } }] ) | For each uniquecust_id, sum the pricefield, results sorted by sum. |
SELECT cust_id, ord_date, SUM(price) AS totalFROMordersGROUPBY 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_dategrouping, sum the pricefield. |
SELECT cust_id,count(*)FROMordersGROUPBYcust_idHAVINGcount(*)> 1 | db.orders.aggregate( [ { $group: { _id: "$cust_id", count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }] ) | For cust_idwith multiple records, return thecust_id and the corresponding record count. |
SELECT cust_id, ord_date, SUM(price) AS totalFROMordersGROUPBY 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)as totalFROM ordersWHEREstatus= 'A'GROUPBY 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)as totalFROM ordersWHEREstatus= 'A'GROUPBYcust_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)as qtyFROM orders o, order_lineitem liWHEREli.order_id= o.idGROUPBYcust_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) asDerivedTable | db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" } } }, { $group: { _id: null, count: { $sum: 1 } } }] ) |
相关文章推荐
- mongodb与sql聚合操作对应图
- MONGODB 与sql聚合操作对应图
- MONGODB 与sql聚合操作对应图
- MONGODB 与sql聚合操作对应图
- MONGODB 与sql聚合操作对应图
- SQL 到 mongo的对应表
- SQL语句CAST()在操作access的时候对应使用函数
- mongodb与sql聚合对应图 M
- sql 的group by order by having 等等与mongo的对应关系
- mongo 的crud操作&&mongo的数据聚合
- [thinkPHP] buildSql可以查看tp CURD操作对应的SQL
- 输出thinkphp中数据库操作对应的sql语句
- Mongodb 3.2 SQL对应聚合 官方
- java代码,使用sql语句操作mongo数据库
- mongo 聚合操作 Java中代码
- 【原创】SQL数据累计求和等聚合操作
- JSP页面四大操作访问MySQL数据库,相对应的sql语句
- mongo 聚合查询sql
- java对mongo的一些去重、分组聚合、分页查询等操作
- 我想在一个表单中操作“档案”表,但其中有“编号”和“名称”两拦,另有“编码”表有两者一一对应,请问怎样用sql在本表单中输入“编号”时,让“名称”拦自动填写?