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')
//查找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'}]
//查找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'}]
//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