http://blog.csdn.net/miyatang/article/details/20997313

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) AS totalFROM 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) AS totalFROM ordersGROUP BY cust_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 ordersGROUP BY 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(*)FROM ordersGROUP 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 ordersGROUP BY 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 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) as totalFROM 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) as qtyFROM orders o, order_lineitem liWHERE li.order_id = o.idGROUP BY cust_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 BY cust_id, ord_date) as DerivedTable

db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" } } }, { $group: { _id: null, count: { $sum: 1 } } }] )

【Mongo】聚合函数的更多相关文章

  1. mongo 聚合函数

    一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...

  2. Mongo聚合函数

    { "_id" : ObjectId("57301c7e5fd5d6e2afa221d1"), "a" : "张三", ...

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

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

  4. 在MongoDB中实现聚合函数

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

  5. 可以这样去理解group by和聚合函数

    写在前面的话:用了好久group by,今天早上一觉醒来,突然感觉group by好陌生,总有个筋别不过来,为什么不能够select * from Table group by id,为什么一定不能是 ...

  6. TSQL 聚合函数忽略NULL值

    max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...

  7. SQL Server 聚合函数算法优化技巧

    Sql server聚合函数在实际工作中应对各种需求使用的还是很广泛的,对于聚合函数的优化自然也就成为了一个重点,一个程序优化的好不好直接决定了这个程序的声明周期.Sql server聚合函数对一组值 ...

  8. Mongodb学习笔记四(Mongodb聚合函数)

    第四章 Mongodb聚合函数 插入 测试数据 ;j<;j++){ for(var i=1;i<3;i++){ var person={ Name:"jack"+i, ...

  9. sql语句 之聚合函数

      聚合分析 在访问数据库时,经常需要对表中的某列数据进行统计分析,如求其最大值.最小值.平均值等.所有这些针对表中一列或者多列数据的分析就称为聚合分析. 在SQL中,可以使用聚合函数快速实现数据的聚 ...

  10. oracle数据库函数之============‘’分析函数和聚合函数‘’

    1分析函数 分析函数根据一组行来进行聚合计算,用于计算完成狙击的累积排名等,分析函数为每组记录返回多个行 rank_number() 查询结果按照次序排列,不存在并列和站位的情况,可以用于做Oracl ...

随机推荐

  1. jQuery 复选框全选/取消全选/反选

    jQuery实现的复选框全选/取消全选/反选及获得选择的值. 完整代码: <!DOCTYPE html> <html> <head> <script type ...

  2. HTML5背景音乐的暂停与播放

    HTML代码:     <audio id="myaudio" loop="loop" preload="auto" autoplay ...

  3. SGMII 和 Serdes 的详细说明

    Serdes  : SERDES是英文SERializer(串行器)/DESerializer(解串器)的简称.它是一种时分多路复用(TDM).点对点的通信技术,即在发送端多路低速并行信号被转换成高速 ...

  4. 黑客编程教程(十三)多线程DOS程序

    DOS基本原理相信大家都已经很熟悉了,DOS工具大家也用的很熟悉.在群里 经常有人说什么时候去DOS什么东西. 现在我们就自己编写一个DOS工具. #include <winsock2.h> ...

  5. 每日英语:How to Be a Better Conversationalist

    Jason Swett still cringes when he remembers the party in Atlanta 10 years ago, where, drink in hand, ...

  6. Oracle快速测试连接是否成功

    Oracle 客户端建立了TNS连接后,可以快速查看连接是否成功. 1.在cmd中执行命令tnsping orcl(全局数据库名称),即可.以下是命令执行后的示例. 2.如果上一步成功,可以进一步执行 ...

  7. Extjs加载本地的一个json文件

    <head>    <title></title>    <link href="../Styles/Extjs/resources/css/ext ...

  8. 过滤4字节及以上的字符c++实现

    这个是根据php的一个版本改的,用来处理utf-8编码的多字节字符,比如中文,俄文等等. #include <iostream> #include <string> int s ...

  9. Python 部署 flask 用uwsgi和nginx

    安装uwsgi nginx 具体方法请百度 1.在项目目录下建立.ini文件 xad_uwsgi.ini [uwsgi] master=true #项目目录 chdir=/root/zhaoyingj ...

  10. c++多继承浅析

            图一                                                                                       图二 ...