【Mongo】聚合函数
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】聚合函数的更多相关文章
- mongo 聚合函数
一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...
- Mongo聚合函数
{ "_id" : ObjectId("57301c7e5fd5d6e2afa221d1"), "a" : "张三", ...
- 在MongoDB中实现聚合函数 (转)
随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加.这使得很多组织都在寻找一种经济的 ...
- 在MongoDB中实现聚合函数
在MongoDB中实现聚合函数 随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加. ...
- 可以这样去理解group by和聚合函数
写在前面的话:用了好久group by,今天早上一觉醒来,突然感觉group by好陌生,总有个筋别不过来,为什么不能够select * from Table group by id,为什么一定不能是 ...
- TSQL 聚合函数忽略NULL值
max,min,sum,avg聚合函数会忽略null值,但不代表聚合函数不返回null值,如果表为空表,或聚合列都是null,则返回null.count 聚合函数忽略null值,如果聚合列都是null ...
- SQL Server 聚合函数算法优化技巧
Sql server聚合函数在实际工作中应对各种需求使用的还是很广泛的,对于聚合函数的优化自然也就成为了一个重点,一个程序优化的好不好直接决定了这个程序的声明周期.Sql server聚合函数对一组值 ...
- Mongodb学习笔记四(Mongodb聚合函数)
第四章 Mongodb聚合函数 插入 测试数据 ;j<;j++){ for(var i=1;i<3;i++){ var person={ Name:"jack"+i, ...
- sql语句 之聚合函数
聚合分析 在访问数据库时,经常需要对表中的某列数据进行统计分析,如求其最大值.最小值.平均值等.所有这些针对表中一列或者多列数据的分析就称为聚合分析. 在SQL中,可以使用聚合函数快速实现数据的聚 ...
- oracle数据库函数之============‘’分析函数和聚合函数‘’
1分析函数 分析函数根据一组行来进行聚合计算,用于计算完成狙击的累积排名等,分析函数为每组记录返回多个行 rank_number() 查询结果按照次序排列,不存在并列和站位的情况,可以用于做Oracl ...
随机推荐
- [svc][op]关闭linux centos各种声音
现在基本都用xshell了,直接xshell禁止即可 shell报警 #vi /etc/inputrc ================================ set bell-style ...
- lftp查看文件时间与登录服务查看文件时间相差8小时
第一步,校正VPS时区设置: rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 第二步,修改FT ...
- Sql Server连接字符串
SQL Server .NET Data Provider 连接字符串包含一个由一些属性名/值对组成的集合.每一个属性/值对都由分号隔开. PropertyName1=Value1; ...
- spring boot设定mysql
mysql创建表,制定自增,utf-8编码 create table person(id int auto_increment primary key, name varchar(100), age ...
- Office 2013 Excel 打开文档很慢很慢的解决方法
这个问题查了很多案例,试了很多方法,但是只有下面这个方法有用! 这几天打开excel文档很慢很慢,双击之后好久没反应,过会儿它才慢慢冒出来,当时将就了,刚刚休息的时候想着查一下吧,不然很影响工作效率! ...
- C中fread()函数的返回值
这个问题很容易搞错,并导致很多问题,需要强调的是fread函数返回的并不是字节数. realRead = fread(buf,item,count,fp) (每次读item大小的数据块,分cou ...
- JS学习笔记(6)--音乐播放器
说明(2017.3.15): 1. lrc.js里面存储LRC歌词的格式的数组,获取里面的时间轴,转为秒数. 2. 通过audio.currentTime属性,setinterval每秒获取歌曲播放的 ...
- 基于HTML5/CSS3图片网格动画特效
现在HTML5技术可以让网页上的图片变得非常神奇,各种各样的HTML5图片动画特效让你眼花缭乱.今天要分享的这款HTML5图片网格动画特效就非常炫酷.图片缩略图按网格的布局一行行排列,你只需点击按钮即 ...
- 一款基于css3的简单的鼠标悬停按钮
今天给大家分享一款基于css3的简单的鼠标悬停按钮.这款悬停按钮鼠标经过前边框是间断的.当鼠标经过的时候边框间隔消失.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div ...
- elasticsearch介绍
elasticsearch是一个基于lucene所编写的分布式的搜索引擎,能够达到实时搜索,稳定高效. 我所了解的elasticsearch有下面这些特点 1 配置简单,对于初学者来说几乎是下载后零配 ...