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. [luoguP2518][HAOI2010]计数(数位DP)

    传送门 重新学习数位DP.. 有一个思路,枚举全排列,然后看看比当前数小的有多少个 当然肯定是不行的啦 但是我们可以用排列组合的知识求出全排列的个数 考虑数位dp 套用数位dp的方法,枚举每一位,然后 ...

  2. USACO Runaround Numbers

    题目大意:问最近的比n大的循环数是多少 思路:第n遍暴力大法好 /*{ ID:a4298442 PROB:runround LANG:C++ } */ #include<iostream> ...

  3. 启动uwsgi报错error while loading shared libraries: libpcre.so.1:

    启动uwsgi时候报错: [root@ richie]# /usr/bin/uwsgi --ini /usr/local/nginx/conf/uwsgi.ini /usr/bin/uwsgi: er ...

  4. 【2018.10.20】noip模拟赛Day3 二阶和

    今年BJ省选某题的弱化版…… 这看起来就没那么难了,有几种方法维护,这里提两种. 第一种(傻逼的我写的) 维护 一维&二维前缀和. 对于一个长度为$m$的序列$b_1,b_2,...,b_m$ ...

  5. 【2018.9.20】JOI 2017 Final T2「準急電車 / Semiexpress」

    题目描述 JOI 铁路公司是 JOI 国唯一的铁路公司. 在某条铁路沿线共有 $N$ 座车站,依次编号为 $1...N$. 目前,正在服役的车次按照运行速度可分为两类:高速电车(简称快车)与普通电车( ...

  6. android soap webservers 无法执行 报错 ht.call(SOAP_ACTION, envelope);解决方法

    1.可能没有加入网络访问权限,在Manifest里面加入,<uses-permission android:name="android.permission.INTERNET" ...

  7. golang log日志

    写入日志文件 func main() { file, err := os.Create("test.log") if err != nil { log.Fatalln(" ...

  8. js-判断当前页面是否在微信浏览器中打开

    方案一:推荐 var ua = navigator.userAgent.toLowerCase(); var isWinxin = ua.indexOf('micromessenger') != -1 ...

  9. hadoop 学习(二)

    我们很荣幸能够见证Hadoop十年从无到有,再到称王.感动于技术的日新月异时,希望通过这篇内容深入解读Hadoop的昨天.今天和明天,憧憬下一个十年. 本文分为技术篇.产业篇.应用篇.展望篇四部分 技 ...

  10. spring启动时加载字典表数据放入map

    import java.util.HashMap; import java.util.List; import org.springframework.beans.factory.annotation ...