MONGDB查询操作
介绍下Mongodb查询
查询语句
1.find()
#用法
db.collection_name.find()
#查询不加如何参数默认返回所有数据记录
#.pretty()让结果更美观
db.collcetname.find().pretty()
2.AND条件
当 find() 中传入多个键值对时,MongoDB就会将其作为 AND 查询处理。
#用法:
db.mycol.find(
{ key1: value1, key2: value2 } #一个集合{}里算是AND条件
).pretty()
3.OR条件
MongoDB中,OR 查询语句以 $or 作为关键词
#例子1:
db.mycol.find(
$or:[
{ key1: value1, key2: value2 } , #一个集合{}里算是AND条件
{ key3 : value3} #列表里的集合为或的关系
]
).pretty()
#例子2
db.mycol.find(
$or:[
{ key1 : value1} ,
{ key2 : value2} #列表里的集合为或的关系
]
).pretty()
4.同时使用AND和OR
> db.post.find({
"id": {$gt:10},
$or: [
{"by": "test"},
{"title": "MongoDB Overview"}
]
}).pretty()
5.比较操作符:
{$gt:10} 表示大于10,
$lt 表示小于,(less than)
$lte 表示小于等于, (less than or equal to)
$gte 表示大于等于,(greater than or equal to)
$ne 表示不等于。(not equals)
记忆:
g代表greater l 代表less。 e代表equal