mongodb与sql聚合对应图 M - CSDN博客 http://blog.csdn.net/u011930016/article/details/49422425

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.

实例:
[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) A2S 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 } } }] )

Aggregation — MongoDB Manual https://docs.mongodb.com/manual/aggregation/

Aggregation Pipeline

Map-Reduce

Single Purpose Aggregation Operations

mongodb与sql聚合对应图 M的更多相关文章

  1. mongoDB与sql聚合操作对应图

    SQL Terms, Functions, and Concepts MongoDB Aggregation Operators WHERE $match GROUP BY $group HAVING ...

  2. 在MongoDB中实现聚合函数 (转)

    随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加.这使得很多组织都在寻找一种经济的 ...

  3. 在MongoDB中实现聚合函数

    在MongoDB中实现聚合函数 随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加. ...

  4. SQL 聚合函数

    SQL聚合函数 MAX---最大值 MIN--最小值 AVG--平均值 SUM--求和 COUNT--记录的条数 EXample: --从MyStudent表中查询最大年龄,最小年龄,平均年龄,年龄的 ...

  5. 【MongoDB】MongoDB VS SQL数据库

    MongoDB和SQL数据库都能满足数据库的基本功能:1.有组织的存放数据:2.按照需求查询数据 传统的SQL数据库(e.g.Oracle, MySQL) 对表的运用不够灵活,横向扩展不太容易,而它的 ...

  6. SQL Server数据库--》top关键字,order by排序,distinct去除重复记录,sql聚合函数,模糊查询,通配符,空值处理。。。。

    top关键字:写在select后面 字段的前面 比如你要显示查询的前5条记录,如下所示: select top 5 * from Student 一般情况下,top是和order by连用的 orde ...

  7. MongoDB对应SQL语句

    -------------------MongoDB对应SQL语句------------------- 1.Create and Alter     1.     sql:         crea ...

  8. Navicat Premium 12 破解(MySQL、MariaDB、MongoDB、SQL Server、SQLite)

    打开注入到安装目录中的exe中 破解提示(还没好,继续看下去) 如果你安装的是中文版,选一下中文版(英文默认即可),获取一下key(名字和组织可以自定义) 打开Navicat,选择注册(第一次打开选注 ...

  9. mongodb的sql日志

    在Yii2中是没有打印出mongodb的sql语句,故借用下log来查看吧. 在网上有说可以使用$model->find()->createCommand()->getRawSql( ...

随机推荐

  1. 【转】Eric's并发用户数估算与Little定律的等价性

    转自:http://www.cnblogs.com/hundredsofyears/p/3360305.html 在国内性能测试的领域有一篇几乎被奉为大牛之作的经典文章,一个名叫Eric Man Wo ...

  2. 【Luogu】P2522Problemb(莫比乌斯反演)

    题目链接 同Zip—Queries,但是用到容斥原理 设f(n,m)是(x,y)的对数,其中1<=x<=n,1<=y<=m 则有f(n,m)-f(a-1,n)-f(b-1,m) ...

  3. P1651 塔 (动态规划)

    题目描述 小明很喜欢摆积木,现在他正在玩的积木是由N个木块组成的,他想用这些木块搭出两座高度相同的塔,一座塔的高度是搭建它的所有木块的高度和,并且一座塔至少要用一个木块.每个木块只能用一次,也可以不用 ...

  4. 【单调队列】bzoj 1407 [HAOI2007]理想的正方形

    [题意] 给定一个n*m的矩阵,求所有大小为k*k的正方形中(最大值-最小值)的最小值 [思路] 先横着算出每一行的长度为k的窗口内的最大值,变成一个n*(m-k+1)的矩阵mx 再竖着算出每一列的长 ...

  5. bzoj3609 [Heoi2014]人人尽说江南好 博弈

    [Heoi2014]人人尽说江南好 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 581  Solved: 420[Submit][Status][D ...

  6. Python入门--4--分之和循环

    1.用ELIF比较省CPU: 第一种方法,使用if score = int(input('请输入你的分数:')) if (score <= 100) and (score >= 90): ...

  7. hdu4619 / 最大独立集

    题意,一个矩阵,上面可以横放或者竖着放骨牌(1X2)保证横的与横的不重叠,竖的和竖的不重叠,求拿掉最小的牌,使所有的都不重叠. 分析:一看,不重叠就是没有边,拿最少,就是留最多,最大独立集啊!二分图, ...

  8. css3 nth-child 与 nth-of-type 的区别

    by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=1709 一.深呼吸,直 ...

  9. IOC基本理解

    什么是IOC? IOC全称为控制反转(Inversion Of Control),别名依赖注入(Dependency Injection). 控制反转即指我们获取依赖的方式发生了反转. 假设存在如下情 ...

  10. MD5进行文件完整性校验的操作方法

    我组产品包含大量音频和图片资源,MD5主要就用来检测这些资源文件的完整性.主要思路是:先计算出所有资源文件的MD5值,存到一个xml文件中,作为标准的MD5值.然后把这个xml文件放到我们的产品中,每 ...