工具推薦:Robomongo,可自行百度尋找下載源,個人比較推薦這個工具,相比較mongoVUE則更加靈活。
集合簡單查詢方法
mongodb語法:db.collection.find() //collection就是集合的名稱,這個可以自己進行創建。
對比sql語句:select * from collection;
查詢集合中所有的文檔,即關系型數據庫中的查詢表中的所有數據。
返回制定的鍵值
mongodb語法:db.collection.find({},{"userid":1})
對比sql語句:select userid from collection;
條件過濾
mongodb語法 : db.collection.find({"userid":495})
對比sql語句:select * from collectionwhere userid = 495;
查詢全格式書寫解釋
db.collection.find({},{})
第一個{}中,寫入的都是相當於sql語句中where后的條件,多條件格式為{"key":value,"key2":"value2"}
第二個{}中,寫入的都是相當於sql語句中跟在select后邊的具體字段,格式為{"key":value,"key2":value}
當value = 0時為不顯示此字段值,當value !=0,即等於任何非0值時,則為顯示此字段。
例:
mongodb查詢:
db.error.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1})
sql查詢:
select userid,type,message from error where userid=1 and type = "debug";
sort排序與limit返回固定條目數
mongodb查詢:
db.collection.find({"userid":1,"type":"debug"},{"userid":1,"type":1,"myssage":1}).sort("time":-1).limit(10)
sql查詢:
select userid,type,message from collection where userid=1 and type = "debug" order by time desc limit 10;
count返回結果集總數
mongodb查詢:
db.collection.count()
sql查詢:
select count(*) from collection;
查詢操作符"$gt" -->大於操作符
mongodb查詢:
db.collection.find({"userid":{"$gt":"494"}})
sql查詢:
select * from collection where userid > 494;
查詢操作符"$gte" -->大於等於
mongodb查詢:
db.collection.find({"userid":{"$gte":"494"}})
sql查詢:
select * from collection where userid >= 494;
查詢操作符 "$lt"-->小於
mongodb查詢:
db.collection.find({"userid":{"$lt":"494"}})
sql查詢:
select * from collection where userid <494;
查詢操作符"$lte"-->小於等於
mongodb查詢:
db.collection.find({"userid":{"$lte":"494"}})
sql查詢:
select * from collection where userid < =494;
查詢操作符"$ne"-->不等於
mongodb查詢:
db.collection.find({"userid":{"$ne":"494"}})
sql查詢:
select * from collection where userid != 494;
查詢操作符"null查詢"-->空
mongodb查詢:
db.collection.find({"userid":null})
sql查詢:
select * from collection where userid is null;
查詢操作符"$all"-->匹配所有
mongodb查詢:
db.collection.find({"userid":{"$all":"494"}})
sql查詢:
select * from collection where userid = 494;
當文檔類型為數組時,使用$all進行匹配,非數組類型使用時與單一匹配一樣。
查詢操作符"$size"-->用於數組查詢,查詢指定長度的數組
mongodb查詢:
db.collection.find({"remark":{"$size":"3"}})
查詢操作符"$in"--> 在范圍內
mongodb查詢:
db.collection.find({"userid":{"$in":["297","495"]}})
sql查詢:
select * from collection where userid in (297,495);
查詢操作符"$nin"-->不在范圍內
mongodb查詢:
db.collection.find({"userid":{"$nin":["297","495"]}})
sql查詢:
select * from collection where userid not in (297,495);
查詢操作符"$and"-->至少包含兩個表達式,兩個表達式都滿足的文檔返回
mongodb查詢:
db.collection.find({"$and":[{"userid":"495"},{"type":"info"}]})
sql查詢:
select * from collection where userid=495 and type='info';
查詢操作符"$nor"-->至少包含兩個表達式,兩個表達式都不滿足的文檔返回
mongodb查詢:
db.collection.find({"$nor":[{"userid":"495"},{"userid":"297"}]})
sql查詢:
select * from collection where userid not in (297,495);
查詢操作符"$not"-->找出不匹配表達式的文檔,不能夠單獨使用,必須與其他表達式配合使用
mongodb查詢:
db.collection.find({"userid":{"$not":{"$gt":"297"}}})
等同於:db.collection.find({"userid":{"$lte":"297"}}})
sql查詢:
select * from collection where userid <=297;
查詢操作符"$or"-->至少包含兩個表達式,兩個表達式至少滿足一個的文檔返回
mongodb查詢:
db.collection.find({"$or":[{"userid":"495"},{"userid":"297"}]})
sql查詢:
select * from collection where userid =297 or userid = 495;
查詢操作符"$exists"-->查詢文檔中字段是否存在
mongodb查詢:
db.collection.find({"$exists":"true"})
查詢操作符"$mod"-->鍵值對變量參數取模,值等於另一個參數
mongodb查詢:
db.collection.find({"userid":{"$mod":[10,7]}})
執行條件:userid字段值必須是數字,userid對10取模,值等於7的文檔返回。
sql查詢:
select * from collection where (user_id%10)=7
查詢操作符"$regex"-->正則匹配
mongodb查詢:
db.collection.find({"userid":/5$/})
sql查詢:
select * from collection where userid like '%5';
sql正則寫法:
查詢操作符"$slice"-->控制返回的數組元素中的元素個數
mongodb查詢:
db.collection.find({},{"remark":{"$slice":5})
remark數組鍵值,返回數組鍵值中的前5個元素
db.collection.find({},{"remark":{"$slice":[10,5]})
remark數組鍵值,返回數組鍵值中的第11到15個元素,偏移量為10,然后返回5個。
db.collection.find({},{"remark":{"$slice":-5})
remark數組鍵值,返回數組鍵值中的后5個元素
逐漸補充中......