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. [na][dhcp]华为DHCP-重要

    近日遇到遇到控制器和wac对接的一些问题.尤其是地址池这块排查起来比较费事,且这些命令不容易找到,以下是能经常用到的命令. 1,查看ip是否冲突: (看下conflict字段) 2,防止冲突命令: 3 ...

  2. 更新yum源却忘了生成缓存 造成每次启动机器报:the package list needs to be rebuilt

    更新yum源的后一定要执行下面的两条命令: yum clean all yum makecache  注意:如果有第三方源的时候是,开机发果不联网的时候,也会报这个错!!!!!

  3. C++ 11 创建和使用 shared_ptr

    shared_ptr 的类型是C + +标准库中一个聪明的指针,是为多个拥有者管理内存中对象的生命周期而设计的.在你初始化一个 shared_ptr 后,你可以复制它,把函数参数的值递给它,并把它分配 ...

  4. 字符编码:ASCII,Unicode,UTF-8

    1.ASCII码美国制定的一套字符编码,对英语字符和二进制位之间的关系,做了统一规定.ASCII码一共规定了128个字符(包括32个不能打印出来的控制符号)的编码,占用一个字节,字节的最前面1位统一为 ...

  5. ny236 心急的C小加 hdoj1051 Wooden Sticks

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间 ...

  6. LeetCode: Wildcard Matching 解题报告

    Wildcard MatchingImplement wildcard pattern matching with support for '?' and '*'. '?' Matches any s ...

  7. Python获取时间范围内日期列表和周列表的函数

    1.获取日期列表 # -*- coding=utf-8 -*- import datetime def dateRange(beginDate, endDate): dates = [] dt = d ...

  8. eclipse javaee 插件安装

    eclipese 精简版安装java ee插件 , 按图走  (eclipse 版本 : Indigo Service Release 1   (3.7.1)) java ee 在线安装地址: htt ...

  9. mongodb常见管理命令

    ----------1.复制数据库 wind:PRIMARY> show dbs; jinri 0.078GB local 1.078GB test 0.078GB wind 0.078GB w ...

  10. 【WPF】对话框/消息弹窗

    非模式对话框 需求:弹窗是非模式对话框,即可以多个弹窗弹出,且弹窗后面的窗体可以被操作,不会被锁定. 自定义的窗体Window实现以下步骤: 在C#代码中弹出窗体时,使用 window.Show() ...