推荐两个网站:英文版本的实例及使用说明,很实用呦~~
https://docs.mongodb.com/manual/tutorial/query-documents/
https://docs.mongodb.com/manual/reference/operator/query/
使用一个工具可以连接MongoDB数据库:
,提供了一些简单的操作方式
废话不多说,开始几个常用的介绍:
查询部分:
1.Queries
(1)按照条件查询:
1)无条件群全表查询:
db.getCollection('userinfo').find()
相当于select * from userinfo
2)where 条件:
db.getCollection('userinfo').find({"age":27})
相当于select * from userinfo where age='27'
3)and 条件:
db.getCollection('userinfo').find({"age":27,"name":"xing"})
相当于select * from userinfo where age='27' and name="xing"
db.getCollection('userinfo').find({},{"name":1})
相当于 select name from userinfo,如果name:0则是不显示name字段
4)limit条件:
db.getCollection('userinfo').find().limit(1)
相当于select * from userinfo limit 1
5)排序Order by 降序 dsec条件:
db.getCollection('userinfo').find().sort({_id:-1})
相当于select * from userinfo order by _id desc
按_id倒序取数据 1为正序,多个条件用,号分开如{name:1,age:-1}
6)分组Order by+排序条件:
db.getCollection('userinfo').find().sort({_id:-1}).limit(1)
相当于select * from userinfo order by _id desc limit 1(降序)
7)区间数据条数limit(a,b):
db.getCollection('userinfo').find().skip(10).limit(20)
相当于select * from userinfo limit 10,20
注意:skip用的时候,一定要注意要是数量多的话skip就会变的很慢,所有的数据库都存在此问题,可以不用skip进行分页,用最后一条记录做为条件。
8)区间值(a,b)条件:
db.getCollection('userinfo').find({"age":{"$gte":27,"$lte":50}})
相当于:select * from userinfo where age>=27 and age<=50
9)>条件:
$gt >
db.getCollection('examinfo').find({'Et_id':{$gt:1}})
相当于:select * from examinfo where Et_id >1
注意:这里比较的值是int型,所以不需要带引号
示例:
10)>=条件:
$gte (>=)
db.getCollection('examinfo').find({'Et_id':{$gte:1}})
相当于:select * from examinfo where Et_id >=1
11)<条件:
$lt (<)
db.getCollection('examinfo').find({'Et_id':{$lt:1}})
相当于:select * from examinfo where Et_id <1
12)<=条件:
$lte (<=)
db.getCollection('examinfo').find({'Et_id':{$lte:1}})
相当于:select * from examinfo where Et_id <=1
13)!=条件:not equal
$ne (!=)
db.getCollection('examinfo').find({'Et_id':{$ne:1}})
相当于:select * from examinfo where Et_id !=1
14)$in 条件:
db.getCollection('userInfo').find({"_id":{"$in":[12,3,100]}})
[ ] 列举出查询的结果,关系为 either ... or
相当于select * from userInfo where _id in (12,3,100)
15)$nin: not in
db.getCollection('userInfo').find({"_id":{"$nin":[12,3,100]}})
相当于select * from userInfo where _id not in (12,3,100)
16)$all: all
[ ] 列举出查询的结果,关系为 both ... and,每一个条件都要符合
db.getCollection('userInfo').find({'UserType':{"$all":[{"0"},{"1"},{"2"}]}})
17)$not: 反匹配:
db.getCollection('userInfo').find({"id_num":{"$not":{"$mod":[5,1]}}})
取的是id_num mod 5 != 1 的字段,比如除了id_num=1,6,11,16等所有字段
18) or 条件:
db.getCollection('userInfo').find({"$or":[{"age":16},{"name":"xing"}]})
相当于select * from userInfo where age = 16 or name = 'xing'
19)组合查询:
查询 Start_time> '2018-01-01' and Start_time<= '2018-12-12' 的数据
db.getCollection('examinfo').find({'Start_time':{$gt:new Date(2018,01,01),$lte:new Date(2018,12,12)}})
20)计算取模查询:
db.getCollection.find({"id_num":{"$mod":[5,1]}})
取的是id_num mod 5 = 1 的字段,如id_num=1,6,11,16
(2)判断字段是否存在:
1)字段存在:
db.getCollection('userInfo').find({ a : { $exists : true }});
// 如果存在元素a,就返回
2)字段不存在:
db.getCollection('userInfo').find({ a : { $exists : false }});
// 如果不存在元素a,就返回
(3)判断字段类型:
1)字符型string:
查询所有name字段是字符类型的
db.getCollection('userInfo').find({name: {$type: 2}});
2)整型 int:
查询所有age字段是整型的
db.getCollection('userInfo').find({age: {$type: 16}})
3) 查询null:
db.getCollection('userInfo').find({"z":null})
//返回没有z字段的所有记录
4)不区分大小写查询:
db.getCollection('userInfo').find({"name":/^joe/i})
//查找name=joe的所有记录,不区分大小写
(4)查询数组:
1)all查询:
db.getCollection('fruit').find({"fruit":{"$all":["苹果","桃子","梨"]}})
// fruit中必需有数组中的每一个才符合结果
2)size数组长度查询:
db.getCollection('fruit').find({"fruit":{"$size":3}})
// fruit数组长度为3的符合结果
3)slice偏移位置:
$slice 可以按偏移量返回记录,针对数组。
如{"$slice":10}返回前10条,{"$slice":{[23,10]}}从24条取10条
4)elemMatch:
如果对象有一个元素是数组,那么$elemMatch可以匹配数组内的元素
db.getCollection('people').find({"name.first":"joe","name.last":"schmoe"})
子查询如:{"id":34,"name":{"first":"joe","last":"schmoe"}}
db.getCollection('people').find({"comments":{"$elemMatch":{"author":"joe","score":{"$gte":5}}}})
查joe发表的5分以上的评论,注意comments为二维数组
$where 在走投无路的时候可以用,但它的效率是很低的。
(5)游标用法:
cursor.hasNext()检查是否有后续结果存在,然后用cursor.next()将其获得。
>while(cursor.hasNext()){
var obj = cursor.next();
//do same
}
(6)去重查询:
1)distinct去重:
db.getCollection('userinfo').distinct('name') 查指定的列,并去重
查询去重name值