mongoDB与sql聚合操作对应图


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) AS 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 } } }] )

mongoDB与sql聚合操作对应图,布布扣,bubuko.com

时间: 2024-08-26 05:16:49

mongoDB与sql聚合操作对应图的相关文章

MONGODB 与sql聚合操作对应图

MongoDB 高级查询条件操作符 2012-04-25 15:35:19|  分类: MongoDB |  标签:mongodb使用  mongodb查询  |举报|字号 订阅 http://blog.163.com/wm_at163/blog/static/13217349020123253346620/ http://blog.csdn.net/yczz/article/details/5978800 下载LOFTER我的照片书  | MongoDB 支持多种复杂的查询方式,能实现大多数

MongoDB中的聚合操作

根据MongoDB的文档描述,在MongoDB的聚合操作中,有以下五个聚合命令. 其中,count.distinct和group会提供很基本的功能,至于其他的高级聚合功能(sum.average.max.min),就需要通过mapReduce来实现了. 在MongoDB2.2版本以后,引入了新的聚合框架(聚合管道,aggregation pipeline ,使用aggregate命令),是一种基于管道概念的数据聚合操作. Name Description count Counts the num

Ruby操作MongoDB(进阶八)-聚合操作Aggregation

上篇博文讲述了排序规则collations的操作和设置方式.顺带介绍了一部分聚合aggregation的设置方式.本文继续介绍聚合操作. 聚合框架的操作处理完数据记录后在返回计算结果.集合操作将来源于多个文档的值归类到一起,这样就可疑在被归类的数据上进行多种操作,然后返回一个单独的结果 1 聚合管道 聚合管道是用于数据聚合的一个框架,是以数据处理管道概念为原型.将文档输入一个多级管道后,可疑将文档转换为聚合的结果.下面以restaurants作为数据集,通过将餐馆类归类,我们就可以使用聚合管道在

MongoDB数据库sql命令操作

概念 RDBMS(关系型数据库管理系统) MongoDB Database(数据库) Database(数据库) Table(表) Collection(集合) Record(记录) Document(文档) 库级操作 use DATABASE #切换/创建库 show dbs #查看所有数据库(空库不会显示) db.dropDatabase() #删除当前数据库 db #查看当前所在库 #集合操作 db.createCollection(name, options) #创建集合 用引号引起来

mongodb 聚合操作

1.首先举例分析下 mongodb 的聚合操作: 该操作表示根据whoisserver_id 字段分组 来统计每个分组下的 count数量: db.anhui.aggregate({$group:{_id:'$whoisserver_id',total:{$sum:1}}}) 查询出来的结果如下: 如果查询总的数量: db.anhui.aggregate({$group:{_id:null,total:{$sum:1}}}) 以下查询先根据条件过滤然后统计 db.anhui.aggregate(

基于Morphia实现MongoDB按小时、按天聚合操作

MongoDB按照天数或小时聚合 需求 最近接到需求,需要对用户账户下的设备状态,分别按照天以及小时进行聚合,以此为基础绘制设备状态趋势图. 实现思路是启动定时任务,对各用户的设备状态数据分别按照小时以及天进行聚合,并存储进数据库中供用户后续查询. 涉及到的技术栈分别为:Spring Boot,MongoDB,Morphia. 数据模型 @Data @Builder @Entity(value = "rawDevStatus", noClassnameStored = true) //

Mongodb聚合操作之读书笔记

Mongodb聚合操作 读书笔记 mongodb,两种计算聚合pipeline和mapreduce pipeline查询速度快于mapreduce,但MapReduce能够在多台Server上并行执行复杂的聚合逻辑. mongodb不允许Pipeline的单个聚合操作占用过多的系统内存,如果一个聚合操作消耗20%以上的内存,那么mongodb直接停止操作,并向客户端输出错误消息. Pipeline方式使用db.collection.aggregate()函数进行聚合运算,运算速度较快,操作简单.

MongoDB 聚合操作(转)

在MongoDB中,有两种方式计算聚合:Pipeline 和 MapReduce.Pipeline查询速度快于MapReduce,但是MapReduce的强大之处在于能够在多台Server上并行执行复杂的聚合逻辑.MongoDB不允许Pipeline的单个聚合操作占用过多的系统内存,如果一个聚合操作消耗20%以上的内存,那么MongoDB直接停止操作,并向客户端输出错误消息. 一,使用 Pipeline 方式计算聚合 Pipeline 方式使用db.collection.aggregate()函

基于 MongoDB 动态字段设计的探索 (二) 聚合操作

业务需求及设计见前文:基于 MongoDB 动态字段设计的探索 根据专业计算各科平均分 (总分.最高分.最低分) public Object avg(String major){ Aggregation aggregation = Aggregation.newAggregation( Aggregation.unwind("courseList"), Aggregation.match(Criteria.where("major").is(major)), Agg