mongoDB与sql聚合操作对应图
|
SQL Terms, Functions, and Concepts |
MongoDB Aggregation Operators |
|
WHERE |
|
|
GROUP BY |
|
|
HAVING |
|
|
SELECT |
|
|
ORDER BY |
|
|
LIMIT |
|
|
SUM() |
|
|
COUNT() |
|
|
join |
No direct corresponding operator; however, the $unwindoperator allows for somewhat similar functionality, but with fields embedded within the document. |
实例:
[td]
|
SQL Example |
MongoDB Example |
Description |
|
SELECT COUNT(*) AS countFROM orders |
db.orders.aggregate( [ { $group: { _id: null, count: { $sum: 1 } } }] ) |
Count all records fromorders |
|
SELECT SUM(price) AS totalFROM orders |
db.orders.aggregate( [ { $group: { _id: null, total: { $sum: "$price" } } }] ) |
Sum theprice field from orders,这个非常有用,看官方说明,说_ID是必须,但没想到可以为NULL, |
|
SELECT cust_id, SUM(price) AStotalFROM ordersGROUP BY cust_id |
db.orders.aggregate( [ { $group: { _id: "$cust_id", total: { $sum: "$price" } } }] ) |
For each uniquecust_id, sum the pricefield. |
|
SELECT cust_id, SUM(price) AStotalFROM ordersGROUP BYcust_idORDER BY 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 totalFROM ordersGROUPBY 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(*)FROMordersGROUP BY cust_idHAVING count(*)> 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 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 } } }] ) |
mongoDB与sql聚合操作对应图的更多相关文章
- MONGODB 与sql聚合操作对应图
MongoDB 高级查询条件操作符 2012-04-25 15:35:19| 分类: MongoDB | 标签:mongodb使用 mongodb查询 |举报|字号 订阅 http://blo ...
- mongodb与sql聚合对应图 M
mongodb与sql聚合对应图 M - CSDN博客 http://blog.csdn.net/u011930016/article/details/49422425 SQL Terms, Func ...
- MongoDB 基本操作和聚合操作
一 . MongoDB 基本操作 基本操作可以简单分为查询.插入.更新.删除. 1 文档查询 作用 MySQL SQL MongoDB 所有记录 SELECT * FROM users; db ...
- MongoDB中的聚合操作
根据MongoDB的文档描述,在MongoDB的聚合操作中,有以下五个聚合命令. 其中,count.distinct和group会提供很基本的功能,至于其他的高级聚合功能(sum.average.ma ...
- MongoDB学习笔记——聚合操作之聚合管道(Aggregation Pipeline)
MongoDB聚合管道 使用聚合管道可以对集合中的文档进行变换和组合. 管道是由一个个功能节点组成的,这些节点用管道操作符来进行表示.聚合管道以一个集合中的所有文档作为开始,然后这些文档从一个操作节点 ...
- MongoDB学习笔记——聚合操作之MapReduce
MapReduce MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,value) ...
- MongoDB学习笔记——聚合操作之group,distinct,count
单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...
- MongoDB之三(高级操作 聚合、游标)
一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...
- MongoDB入门---聚合操作&管道操作符&索引的使用
经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...
随机推荐
- 机器学习笔记(4)Logistic回归
模型介绍 对于分类问题,其得到的结果值是离散的,所以通常情况下,不适合使用线性回归方法进行模拟. 所以提出Logistic回归模型. 其假设函数如下: \[ h_θ(x)=g(θ^Tx) \] 函数g ...
- 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem I. Integral Polygons
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229510 时间限制:2s 空间限制:256MB 题目大意: 给定一个凸多边形,有一种连接两个 ...
- HDU 5286 How far away ? lca
题目链接: 题目 How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 03_Java基础语法_第3天(Scanner、Random、流程控制语句)_讲义
今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scan ...
- excel表中判断A列与B列内容是否相同,相同的话在C列按条件输出!
判断两列数据是否相同,有以下几个函数判断(做笔记于此,方便以后查找): 1.=IF(AND(A4=B4),"相同","") 在C列输出相同字符 2.=IF(A1 ...
- jenkin报错hudson.plugins.git.GitExcept
清除工作空间 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/
- 解决Qt creator无法输入中文
详细的方法来自以下网址: http://my.oschina.net/lieefu/blog/505363?p={{currentPage+1}} 需要说明的几点: 设置qmake 的路径使用自身的路 ...
- DateTime Toxxx() 方法获取时间
直接上代码 static void Main(string[] args) { DateTime time = DateTime.Now; Console.WriteLine("ToFile ...
- 【EF】Entity Framework实现属性映射约定
Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...
- 公告:请访问我的个人博客新站点——www.huangshujia.me
我的个人博客现在在(http://www.huangshujia.me/)cnblog这里不会做任何更新了.或者关注我的个人公众号:碱基矿工