SQL与Mongodb聚合的对应关系(举例说明)
SQL中的聚合函数和Mongodb中的管道相互对应的关系:
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum
join $lookup
例子:
先创建文档,填充数据
/* 0 */
{
"_id" : ObjectId("5812b447311bb4272016496a"),
"cust_id" : "abc123",
"ord_date" : ISODate("2012-11-02T17:04:11.102Z"),
"status" : "A",
"price" : 50,
"items" : [{
"sku" : "xxx",
"qty" : 25,
"price" : 1
}, {
"sku" : "yyy",
"qty" : 25,
"price" : 1
}]
} /* 1 */
{
"_id" : ObjectId("58131494311bb418b058fcba"),
"cust_id" : "a",
"ord_date" : ISODate("2012-11-02T17:04:11.102Z"),
"status" : "B",
"price" : 70,
"items" : [{
"sku" : "xxx",
"qty" : 25,
"price" : 1
}, {
"sku" : "yyy",
"qty" : 25,
"price" : 1
}]
} /* 2 */
{
"_id" : ObjectId("581314b6311bb418b058fcbb"),
"cust_id" : "ab",
"ord_date" : ISODate("2012-11-02T17:04:11.102Z"),
"status" : "E",
"price" : 60,
"items" : [{
"sku" : "xxx",
"qty" : 55,
"price" : 1
}, {
"sku" : "yyy",
"qty" : 25,
"price" : 1
}]
}
例1:
SQL:
SELECT COUNT(*) AS count FROM orders
Mongodb:
db.orders.aggregate([
{
$group:{
_id:null,
count:{$sum:1}
}
}
])

例2:
SQL:
SELECT SUM(price) AS total FROM orders
Mongodb:
db.orders.aggregate(
[
{
$group: {
_id:null,
total:{$sum:"$price"}
}
}
])

例3:
SQL:
SELECT cust_id,SUM(price) AS total FROM orders GROUP BY cust_id
Mongodb:
db.orders.aggregate([
{
$group:
{
_id:"$cust_id",
total:
{
$sum:"$price"
}
}
},
{ $sort:
{
total:1
}
} ])

例4:
SQL:
SELECT cust_id, ord_date,SUM(price) AS total FROM orders GROUP BY cust_id, ord_date
Mongodb:
db.orders.aggregate([
{
$group:
{
_id:
{
cust_id:"$cust_id",
ord_date:
{
month:{$month:"$ord_date"},
day:{$dayOfMonth:"$ord_date"},
year:{$year:"$ord_date"}
}
},
total:{$sum:"$price"} }
}
])

例5:
SQL:
SELECT cust_id,count(*) FROM orders GROUP BY cust_id HAVING count(*) > 1
Mongodb:
db.orders.aggregate([
{
$group:{_id:"$cust_id",
count:{$sum:1}
}
},
{$match:{count:{$gt:1}}} ])

例6:
SQL:
SELECT cust_id,ord_date,SUM(price) AS total FROM orders GROUP BY cust_id,ord_date HAVING total > 250
Mongodb:
db.orders.aggregate( [
{
$group: {
_id: {
cust_id: "$cust_id",
ord_date: {
month: { $month: "$ord_date" },
day: { $dayOfMonth: "$ord_date" },
year: { $year: "$ord_date"}
}
},
total: { $sum: "$price" }
}
},
{ $match: { total: { $gt: 250 } } }
] )

例6:
SQL:
SELECT cust_id,SUM(price) as total FROM orders WHERE status = 'A' GROUP BY cust_id
Mongodb:
db.orders.aggregate([
{$match:{status:'A'}},
{$group:{_id:"$cust_id",total:{$sum:"$price"}}}
])

例7:
SQL:
SELECT cust_id,SUM(price) as total FROM orders WHERE status = 'A' GROUP BY cust_id HAVING total > 250
Mongodb:
db.orders.aggregate([
{ $match: { status: 'A' } },
{$group:{_id:"$cust_id",total:{$sum:"$price"}}},
{$match:{total:{$gt:250}}}
])

例8:
SQL:
SELECT cust_id,SUM(li.qty) as qty FROM orders o, order_lineitem li WHERE li.order_id = o.id GROUP BY cust_id
Mongodb:
$unwind的作用是将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值
假如我们的需求是统计每个items出现的次数
这个时候就需要用到先将$unwind items拆分,然后根据具体的items来做分组统计
db.orders.aggregate([
{$unwind:"$items"},
{$group:{_id:"$cust_id",qty:{$sum:"$items.qty"}}}
])

例9:
SQL:
SELECT COUNT(*) FROM (SELECT cust_id,ord_date FROM orders GROUP BY cust_id,ord_date) as DerivedTable
Mongodb:
db.orders.aggregate([
{$group:
{
_id:{
cust_id:"$cust_id",
ord_date:{
month:{$month:"$ord_date"},
day:{$dayOfMonth:"$ord_date"},
year:{$year:"$ord_date"}
}
}
}
},
{
$group:{
_id:null,
count:{$sum:1}
}
}
])

格式要注意
db.orders.aggregate([
{$match:{}}, ----where
{$group:{ ----group
_id:排序字段
total:{聚合函数}
}},
{$match:{}} ----having
])
SQL与Mongodb聚合的对应关系(举例说明)的更多相关文章
- MongoDB 聚合管道(Aggregation Pipeline)
管道概念 POSIX多线程的使用方式中, 有一种很重要的方式-----流水线(亦称为"管道")方式,"数据元素"流串行地被一组线程按顺序执行.它的使用架构可参考 ...
- mongodb MongoDB 聚合 group
MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.col ...
- MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- MongoDB聚合
--------------------MongoDB聚合-------------------- 1.aggregate(): 1.概念: 1.简介 ...
- mongodb MongoDB 聚合 group(转)
MongoDB 聚合 MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.col ...
- mongodb聚合 group
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). 基本语法为:db.collection.agg ...
- MongoDB 聚合(管道与表达式)
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*). aggregate() 方法 MongoDB中 ...
- 【Mongodb教程 第十一课 】MongoDB 聚合
聚合操作过程中的数据记录和计算结果返回.聚合操作分组值从多个文档,并可以执行各种操作,分组数据返回单个结果.在SQL COUNT(*)和group by 相当于MongoDB的聚集. aggregat ...
- mongodb聚合查询-aggregate
Mongodb-aggregate 在工作中经常遇到一些mongodb的聚合操作,和mysql对比起来,mongo存储的可以是复杂的类型,比如数组,字典等mysql不善于处理的文档型结构,但是mong ...
随机推荐
- 基于spring+quartz的分布式定时任务框架
问题背景 我公司是一个快速发展的创业公司,目前有200人,主要业务是旅游和酒店相关的,应用迭代更新周期比较快,因此,开发人员花费了更多的时间去更=跟上迭代的步伐,而缺乏了对整个系统的把控 没有集群之前 ...
- window10 安装SVN 提示权限问题
http://www.yishimei.cn/network/551.html 经常在网上看到有同学反映,他们在控制面板里卸载软件的时候,总是会出现2502.2503错误代码的问题,并且这个问题大多 ...
- curl 模拟请求get/post
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx24a0ef ...
- 通过队列解决Lucene文件并发创建索引
public sealed class SearchIndexManager { private static readonly SearchIndexManager searchIndexManag ...
- python 学习
python 使用 缩进 代替 C 中的 {} 或 delphi 中的 begin...end 1.help() 显示帮助或 help(<命令>) 2.字符串前加 r 表示原始字符串, ...
- 一次APP测试的感悟
项目经理担责任.产品担责任.测试只需要把测试中发现的问题展示出来.如实反应问题.谁担责任谁有权利决定上不上线.所以他们直接绕过了测试.APP的上线让我学到了很多东西,见识了很多东西,也感悟了很多.这是 ...
- JS表单验证
1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...
- VS2012一打开就停止工作的解决方法
昨天刚装好VS2012,用得好好的,嘿,今早儿 一打开程序,就告诉我 VS2012停止工作,只有关闭程序或者调试程序. 小主酸菜郁闷不已,必须找到解决方法啊,错误提示如图: 错误原因: ...
- flag+文件操作
flag标志位,标识位,在其他语言中可能叫开关,个人觉得当作开关更容易理解.下面我们来利用这个开关来控制文件操作的流程,从而优雅的修改配置文件. global log 127.0.0.1 local2 ...
- Bootstrap Affix(附加导航(Affix)插件的用法)
原文网址:http://www.runoob.com/bootstrap/bootstrap-affix-plugin.html Bootstrap 附加导航(Affix)插件 附加导航(Affix) ...