MONGODB 与sql聚合操作对应图
MongoDB 支持多种复杂的查询方式,能实现大多数 T-SQL 功能,远不是 Key-Value 之类的 NoSQL DB 所能比拟的。
Conditional Operators : $slice //切片
Conditional Operators : $lt <, $lte <=, $gt >, $gte >=
Conditional Operator : $ne //不等于
Conditional Operator : $in //属于
Conditional Operator : $nin //不属于
Conditional Operator : $mod //取模运算
Conditional Operator: $all //全部属于
Conditional Operator : $size //数量
Conditional Operator: $exists //字段存在
Conditional Operator: $type //字段类型
Conditional Operator: $or // 或
Regular Expressions //正则表达式
Value in an Array // 数组中的值
Conditional Operator: $elemMatch //要素符合
Value in an Embedded Object //内嵌对象中的值
Meta operator: $not //不是
Javascript Expressions and $where //
sort() //排序
limit() //限制取数据条数
skip() //跳过一定数值开始取
snapshot() //
count() // 数量
group() //分组
准备数据
In [9]: db
Out[9]: Database(Connection('localhost', 27017), u'test')
In [10]: table = db.table_abeen
In [11]: table
Out[11]: Collection(Database(Connection('localhost', 27017), u'test'), u'table_abeen')
In [12]: table.insert({"name":"abeen", "age":27})
Sun Aug 8 23:14:20 connection accepted from 127.0.0.1:46143 #27
Out[12]: ObjectId('4c5f9cbc421aa90fb9000000')
In [14]: table.insert({"name":"shanshan", "age":22})
Out[14]: ObjectId('4c5f9ccb421aa90fb9000001')
Conditional Operator: $ne (not equal)
//查找name不等于abeen的信息
In [24]: list(table.find({"name":{"$ne":"abeen"}}))
Out[24]:
[{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'}]
Conditional Operator: $gt $lt(gt= greater than, lt=less than)
//查找name不等于abeen,并且age大于22的
In [29]: list(table.find({"name": {"$ne": "abeen"}, "age":{"$gt": 22}}))
Out[29]:
[{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'}]
获取子集 $ne $slice
//select "age" from table where name = "abeen"
In [42]: list(table.find({"name": "abeen"}, {"age" : 1}))
Out[42]: [{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27}]
//get all posts about mongodb without "age"
In [43]: list(table.find({"name": "abeen"}, {"age" : 0}))
Out[43]: [{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'name': u'abeen'}]
//name不等于abeen的"age"信息,取前5条
In [48]: list(table.find({"name": {"$ne":"abeen"}}, {"age":{"$slice":5}}))
//取name信息,从第10条开始取20条
In [54]: list(table.find({}, {"name": {"$slice": [10,20]}}))
//取name信息,从后20条开始取10条
In [55]: list(table.find({}, {"name": {"$slice": [-20,10]}}))
取数值范围
//age大于23的
In [56]: list(table.find({"age":{"$gt":23}}))
Out[56]: [{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'}]
//age小于23的
In [57]: list(table.find({"age":{"$lt":23}}))
Out[57]:
[{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'}]
//age大于等于23的
In [58]: list(table.find({"age":{"$gte":23}}))
Out[58]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'}]
//age小于等于23的
In [59]: list(table.find({"age":{"$lte":23}}))
Out[59]:
[{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'}]
Conditional Operator: $gt
//22 < age < 25的
In [63]: list(table.find({"age": {"$gt":22, "$lt":25}}))
Out[63]:
[{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'}]
Conditional Operator : $in
//name在列表["abeen","ab","b"]里面的
In [67]: list(table.find({"name":{"$in":["abeen","ab","b"]}}))
Out[67]: [{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'}]
//name在列表["abeen","ab","b"]里面的,限制取1条数据
In [69]: list(table.find({"name":{"$in":["abeen","ab","b","shanshan"]}}).limit(1))
Out[69]: [{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'}]
Conditional Operator : $nin (not in)
//name不在列表["abeen","ab","b"]里面的
In [70]: list(table.find({"name":{"$nin":["abeen","ab","b"]}}))
Out[70]:
[{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'}]
Conditional Operator: $mod
// 查找age除10模等于1的
In [71]: list(table.find({"age":{"$mod":[10,1]}}))
Conditional Operator: $all
//取name包含所有["abeen","a","b"]的信息
In [77]: list(table.find({"name":{"$all":["abeen","a","b"]}}))
Out[77]:
[{u'_id': ObjectId('4c5facc6421aa90fb9000005'),
u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']}]
Conditional Operator: $size
//取name元素数和$size数相同的信息
In [81]: list(table.find({"name":{"$size": 6}}))
Out[81]:
[{u'_id': ObjectId('4c5facc6421aa90fb9000005'),
u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']}]
Conditional Operator: $exists
//取name存在的信息
In [83]: list(table.find({"name":{"$exists": True}}))
Out[83]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5facc6421aa90fb9000005'),
u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']}]
//取name不存在信息
In [84]: list(table.find({"name":{"$exists": False}}))
Out[84]: []
Conditional Operator: $type
//name类型为字符串的
In [88]: list(table.find({"name":{"$type": 2}}))
type对应该类型表如下:
//查找name等于abeen或等于shanshan的信息
In [95]: list(table.find({"$or" :[{"name": "abeen"}, {"name":"shanshan"}]}))
Out[95]: []
//查找age等于22,或name等于abeen或等于shanshan的信息
In [96]: list(table.find({"age":22, "$or" :[{"name": "abeen"}, {"name":"shanshan"}]}))
Out[96]: []
Regular Expressions
//利用正则查询
In [114]: list(table.find({"name": {"$regex": r".*ee.*"}}))
Out[114]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5facc6421aa90fb9000005'),
u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']}]
正则表达式标记:
i: 忽略大小写。
m: 默认为单行处理,此标记表示多行。
x: 扩展。
Conditional Operator: $elemMatch
In [135]: list(table.find( { "age" : {"$elemMatch": {"name": {"$regex": r".*ee.*"},"age":{"$gt":22}}}}))
Value in an Embedded Object
//查找内部对象信息,
//查找内部对象info的name等于abeen的信息
In [217]: list(table.find({"info.name": "abeen"}))
Out[217]:
[{u'_id': ObjectId('4c5fcd7e421aa90fb9000007'),
u'info': {u'address': u'beijing', u'age': 28, u'name': u'abeen'},
u'name': u'abeen_object'}]
Meta operator: $not
//查询age不在大于23的范围内的信息
In [160]: list(table.find({"age": {"$not":{"$gt": 23}}}))
Out[160]:
[{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'),
u'age': 22,
u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'),
u'age': 22,
u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'),
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'),
u'address': u'da zhong si',
u'age': 23,
u'name': u'shanshan3'},
{u'_id': ObjectId('4c5facc6421aa90fb9000005'),
u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']}]
Javascript Expressions and $where
//age大于23的
In [164]: list(table.find({"age": {"$gt":23}}))
Out[164]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5fae95421aa90fb9000006'), u'age': 25, u'name': u''}]
//age大于23的
In [165]: list(table.find({"$where": "this.age > 23"}))
Out[165]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5fae95421aa90fb9000006'), u'age': 25, u'name': u''}]
//skip() limit()
In [204]: result = table.find().skip(2).limit(3)
In [205]: for r in result : print r
{u'age': 22, u'_id': ObjectId('4c5f9d2d421aa90fb9000002'), u'name': u'shanshan2'}
{u'age': 23, u'_id': ObjectId('4c5f9d34421aa90fb9000003'), u'name': u'shanshan3'}
{u'age': 23, u'_id': ObjectId('4c5fa2ab421aa90fb9000004'), u'name': u'shanshan3',
u'address': u'da zhong si'}
Mongo Query Language 类似 Sql 语句
$query - 查询类似于sql中的 where
$orderby - 排序{x:1},1为升序 -1为降序
$query - 查询类似于sql中的 where
$orderby - 排序{x:1},1为升序 -1为降序
//select * from table where name="abeen" order by age asc
In [28]: list(table.find({"$query" : {"name": "abeen"}, "$orderby": { "age": 1 }}))
//select * from table where name="abeen" order by age desc
In [29]: list(table.find({"$query" : {"name": "abeen"}, "$orderby": { "age": -1 }}))
//select name, address from table where name like '%a%'
按找指定字段按条件,并可指定排序 1:返回字段,0不返回字段
In [21]: list(t.find({"name":{"$regex": r".*a.*"}},{"name":1, "address":1}))
Out[23]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'name': u'abeen'},
{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'), u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'), u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'), u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'), u'address': u'da zhong si', u'name': u'shanshan3'},
{u'_id': ObjectId('4c5facc6421aa90fb9000005'), u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']},
{u'_id': ObjectId('4c5fcd7e421aa90fb9000007'), u'name': u'abeen_object'},
{u'_id': ObjectId('4c64b432421aa90697000000'), u'address': u'shandong', u'name': u'abeen'},
{u'_id': ObjectId('4c64b438421aa90697000001'), u'address': u'shandong', u'name': u'abeen'},
{u'_id': ObjectId('4c64b43d421aa90697000002'), u'address': u'shandong', u'name': u'abeen'},
{u'_id': ObjectId('4c64b441421aa90697000003'), u'address': u'shandong', u'name': u'abeen'}]
0为不返回字段
In [24]: list(t.find({"name":{"$regex": r".*a.*"}},{"address":0}))
Out[24]:
[{u'_id': ObjectId('4c5f9cbc421aa90fb9000000'), u'age': 27, u'name': u'abeen'},
{u'_id': ObjectId('4c5f9ccb421aa90fb9000001'), u'age': 22, u'name': u'shanshan'},
{u'_id': ObjectId('4c5f9d2d421aa90fb9000002'), u'age': 22, u'name': u'shanshan2'},
{u'_id': ObjectId('4c5f9d34421aa90fb9000003'), u'age': 23, u'name': u'shanshan3'},
{u'_id': ObjectId('4c5fa2ab421aa90fb9000004'), u'age': 23, u'name': u'shanshan3'},
{u'_id': ObjectId('4c5facc6421aa90fb9000005'), u'name': [u'abeen', u'a', u'b', u'e', u'e', u'n']},
{u'_id': ObjectId('4c5fcd7e421aa90fb9000007'), u'name': u'abeen_object'},
{u'_id': ObjectId('4c64b432421aa90697000000'), u'age': 22, u'name': u'abeen'},
{u'_id': ObjectId('4c64b438421aa90697000001'), u'age': 23, u'name': u'abeen'},
{u'_id': ObjectId('4c64b43d421aa90697000002'), u'age': 24, u'name': u'abeen'},
{u'_id': ObjectId('4c64b441421aa90697000003'), u'age': 18, u'name': u'abeen'}]
SQL Terms, Functions, and Concepts |
MongoDB Aggregation Operators |
WHERE |
|
GROUP BY |
|
HAVING |
|
SELECT |
|
ORDER BY |
|
LIMIT |
|
SUM() |
|
COUNT() |
|
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 (SELECTcust_id, ord_date FROM orders GROUP BY cust_id, ord_date) asDerivedTable |
db.orders.aggregate( [ { $group: { _id: { cust_id: "$cust_id", ord_date: "$ord_date" } } }, { $group: { _id: null, count: { $sum: 1 } } }] ) |
MONGODB 与sql聚合操作对应图的更多相关文章
- mongoDB与sql聚合操作对应图
SQL Terms, Functions, and Concepts MongoDB Aggregation Operators WHERE $match GROUP BY $group HAVING ...
- mongodb与sql聚合对应图 M
mongodb与sql聚合对应图 M - CSDN博客 http://blog.csdn.net/u011930016/article/details/49422425 SQL Terms, Func ...
- MongoDB 基本操作和聚合操作
一 . MongoDB 基本操作 基本操作可以简单分为查询.插入.更新.删除. 1 文档查询 作用 MySQL SQL MongoDB 所有记录 SELECT * FROM users; db ...
- MongoDB中的聚合操作
根据MongoDB的文档描述,在MongoDB的聚合操作中,有以下五个聚合命令. 其中,count.distinct和group会提供很基本的功能,至于其他的高级聚合功能(sum.average.ma ...
- MongoDB学习笔记——聚合操作之聚合管道(Aggregation Pipeline)
MongoDB聚合管道 使用聚合管道可以对集合中的文档进行变换和组合. 管道是由一个个功能节点组成的,这些节点用管道操作符来进行表示.聚合管道以一个集合中的所有文档作为开始,然后这些文档从一个操作节点 ...
- MongoDB学习笔记——聚合操作之MapReduce
MapReduce MongoDB中的MapReduce相当于关系数据库中的group by.使用MapReduce要实现两个函数Map和Reduce函数.Map函数调用emit(key,value) ...
- MongoDB学习笔记——聚合操作之group,distinct,count
单独的聚合命令(group,distinct,count) 单独聚合命令 比aggregate性能低,比Map-reduce灵活度低:但是可以节省几行javascript代码,后面那句话我自己加的,哈 ...
- MongoDB之三(高级操作 聚合、游标)
一: 聚合 常见的聚合操作跟sql server一样,有:count,distinct,group,mapReduce. <1> count count是最简单,最容易,也是最常用的聚合工 ...
- MongoDB入门---聚合操作&管道操作符&索引的使用
经过前段时间的学习呢,我们对MongoDB有了一个大概的了解,接下来就要开始使用稍稍深入一点的东西了,首先呢,就是MongoDB中的聚合函数,跟mysql中的count等函数差不多.话不多说哈,我们先 ...
随机推荐
- mfc ui3
引用:http://www.cnblogs.com/rainbowzc/archive/2010/06/02/1749872.html 本文专题讨论VC中的界面美化,适用于具有中等VC水平的读者.读者 ...
- python学习笔记系列----(一)python简介
一个月前,就按下决心要系统的学习下python了,虽然之前有学习过java,学习过c++,也能较为熟练的使用java做自动化测试看懂c++里的业务逻辑,但是实际上有那么多的东西自己还是不清楚,今天下定 ...
- web页面状态
200 :表示请求成功,服务器已处理 201 :表示服务器端请求被创建, 202 :表示服务器端请求已经被接收,但未处理完成 400 :表示错误的请求 404:表示请求的资源未找到. 405:表示没有 ...
- eclipse启动时虚拟机初始内存配置
eclipse启动时虚拟机初始内存配置: -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m
- 【Office Word】论文排版有关技巧
本文分两部分,第一部分呢是Word中标题的编号以及图表的编号:第二部分是MathType中公式编号的右对齐方法. 1. word中标题的编号以及图表的编号 本部分转载自:http://blog ...
- PetaPoco入门(二)
1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...
- Designing a CSS based template
http://veerle-v2.duoh.com/blog/comments/designing_a_css_based_template_part_i/
- MySQL查询语句
来源于网络... Sutdent表的定义 字段名 字段描述 数据类型 主键 外键 非空 唯一 自增 Id 学号 INT(10) 是 否 是 是 是 Name 姓名 VARCHAR(20) 否 否 是 ...
- iOS,Xcode7 制作Framework,含资源和界面
Xcode7 制作Framework 本文通过Demo方式介绍1)将含bundle和存代码编写界面打包进framework:2)将storyboard +assets.xcassets打包. (一) ...
- C++string中有关字符串内容修改和替换的函数浅析
1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...