目錄
一、Find
>db.test.find() 批量返回集合test中的所有文檔
1.1指定需要返回的鍵
>db.test.find({},{"username":1}) 返回集合test中指定的username鍵(默認情況下“_id”這個鍵總是被返回)
db.test.find({},{'username':1})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c39"), "username" : "user1" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "username" : "user2" }
db.test.find({},{'username':1,'id':1})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c39"), "id" : 1, "username" : "user1" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2" }
如若想剔除返回結果中的某些鍵,可以使用:
db.test.find({},{'username':1,'_id':0})
{ "username" : "user1" }
{ "username" : "user2" }
{ "username" : "user3" }
二、查詢條件
2.1查詢條件
$lt , $lte , $gt , $gte 分別對應 < , <= , >, >=
例如:查詢 id 大於13 的用戶 , 查詢 id 大於 5 小於 10 的用戶
db.test.find({'id':{'$gt':13}})
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c46"), "id" : 14, "username" : "user14", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c47"), "id" : 15, "username" : "user15", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }
db.test.find({'id':{'$gt':5,'$lt':10}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c40"), "id" : 8, "username" : "user8", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c41"), "id" : 9, "username" : "user9", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
還有一種條件操作符 $ne ,他表示不相等;$ne 能用於多有類型的數據
例如:查詢所有名字不爲 user1 的用戶
db.test.find({'username':{'$ne':'user1'}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3b"), "id" : 3, "username" : "user3", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
2.2 OR 查詢
MongoDB中有兩種方式進行OR 查詢: $in 可以用來查詢一個鍵的多個值 ; $or 更通用一些
$in 非常靈活,可以指定不同類型的條件和值
db.test.find({'id':{'$in':[5,6,7]}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3d"), "id" : 5, "username" : "user5", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
db.test.find({'$or':[{'id':6},{'username':'user2'}]})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3e"), "id" : 6, "username" : "user6", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
2.3 $not
$not 是元條件句,即可以用在任何其他條件之上
拿取模運算符 $mod 來說 ,這是查詢用戶id 除以5 餘數不等於 1 的用戶 ,也就是除了 1,6,11
db.test.find({'id':{'$not':{'$mod':[5,1]}}})
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3a"), "id" : 2, "username" : "user2", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3b"), "id" : 3, "username" : "user3", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3c"), "id" : 4, "username" : "user4", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3d"), "id" : 5, "username" : "user5", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c3f"), "id" : 7, "username" : "user7", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c40"), "id" : 8, "username" : "user8", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c41"), "id" : 9, "username" : "user9", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4a0706f20d3c2ec14c42"), "id" : 10, "username" : "user10", "lastUpdate" : "Tue Sep 04 2018 17:01:59 GMT+0800" }
{ "_id" : ObjectId("5b8e4b0006f20d3c2ec14c44"), "id" : 12, "username" : "user12", "lastUpdate" : "Tue Sep 04 2018 17:06:08 GMT+0800" }
三、特定類型的查詢
3.1 null
null 不僅會匹配某個鍵值爲 null 的文檔,而且還會匹配不包含這個鍵的文檔
如果僅想匹配鍵值爲null 的文檔,既要檢查該鍵的值是否爲 null ,還要通過 $exists 條件判定鍵值已存在
db.test.find({'age':{'$in':[null],'$exists':true}})
3.2正則表達式
正則表達式能夠靈活有效地匹配字符串。如果不會可以參考 https://blog.csdn.net/YuiJar/article/details/81316478
查詢user集合中 名字爲 字母、數字、下劃線組成的用戶
db.user.find({'name':/\w+/},{'name':1})
{ "_id" : ObjectId("5b85102593bf31057845081b"), "name" : "zhaoliu" }
{ "_id" : ObjectId("5b860c8328a8c1b0ab5508c1"), "name" : "qqq" }
{ "_id" : ObjectId("5b864e958680f54603428fd2"), "name" : "wangwu" }
注意:mongodb 可以爲前綴型正則表達式(比如:/^joy/)查詢創建索引,所以這種類型的查詢會非常高效
正則表達式也可以匹配自身,但幾乎滅有人會這麼做
3.3查詢數組
查詢數組元素與查詢標量值是一樣的
例如有一個水果列表 >db.food.insert({'fruit':['apple','banana','pear']})
下面的查詢 >db.food.find({'fruit':'banana'})
則會成功匹配該文檔
3.4查詢內嵌文檔
例如有如下文檔:
{
'name':{
'first':'zhang',
'last':'san'
},
'age':15
}
要查找姓名爲 zhangsan 的人可以這樣
>db.people.find({'name':{'first':'zhang','last':'san'}})
我們也可以使用點表示法查詢內嵌的文檔
>db.people.find({'name.first':'zhang','name:last':'san'})
四、where 查詢
在非必要時,儘量避免 where 查詢。where在速度上要比常規的查詢慢很多,而且不安全