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. 九度oj 题目1026:又一版 A+B

    题目描述: 输入两个不超过整型定义的非负10进制整数A和B(<=231-1),输出A+B的m (1 < m <10)进制数. 输入: 输入格式:测试输入包含若干测试用例.每个测试用例 ...

  2. 【Android】android:ellipsize的使用以及一个点解决方法

    EidtText和textview中内容过长的话自动换行,使用android:ellipsize与android:singleine可以解决,使只有一行. EditText不支持marquee 用法如 ...

  3. UITableView点击切换状态分析

    原理:多选+点击动画 初始化和点击都调用的方法 - (void)changeStateAnimated:(BOOL)animated { //不需要动画,初始化的时候 if (animated == ...

  4. BZOJ 1113 Wall ——计算几何

    凸包第一题. 自己认为自己写的是Andrew 其实就是xjb写出来居然过掉了测试. 刚开始把pi定义成了int,调了半天 #include <map> #include <cmath ...

  5. USACO Longest Prefix

    题目大意:给出一个长字符串,问最长的前缀,使得这个前缀能用给出的一些元素组合而成 思路:暴力dp,dp[i]表示长度为i的前缀能否被表示 /*{ ID:a4298442 PROB:prefix LAN ...

  6. Spring-IOC源码解读2.2-BeanDefinition的载入和解析过程

    1. 对IOC容器来说这个载入过程就像是相当于把BeanDefinition定义的信息转化成Spring内部表示的数据结构.容器对bean的管理和依赖注入过程都是通过对其持有的BeanDefiniti ...

  7. 洛谷——P2690 接苹果

    P2690 接苹果 题目背景 USACO 题目描述 很少有人知道奶牛爱吃苹果.农夫约翰的农场上有两棵苹果树(编号为1和2), 每一棵树上都长满了苹果.奶牛贝茜无法摘下树上的苹果,所以她只能等待苹果 从 ...

  8. python的__name__和dir()属性

    1.__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行.示例 ...

  9. UVA11825 Hackers' Crackdown

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 3 2 1 2 2 0 2 2 0 1 4 1 1 1 0 1 3 1 2 0 输出样例#1: Case 1: 3 ...

  10. Windows下部署多个Tomcat

    编辑bin/startup.bat SET JAVA_HOME=...(JDK所在路径) SET CATALINA_HOME=...(Tomcat解压的路径) 编辑server.xml文件 <! ...