工具推薦:Robomongo,可自行百度尋找下載源,個(gè)人比較推薦這個(gè)工具,相比較mongoVUE則更加靈活。
集合簡(jiǎn)單查詢方法 mongodb語(yǔ)法:db.collection.find() //collection就是集合的名稱,這個(gè)可以自己進(jìn)行創(chuàng)建。 對(duì)比sql語(yǔ)句:select * from collection; 查詢集合中所有的文檔,即關(guān)系型數(shù)據(jù)庫(kù)中的查詢表中的所有數(shù)據(jù)。
返回制定的鍵值 mongodb語(yǔ)法:db.collection.find({},{"userid":1}) 對(duì)比sql語(yǔ)句:select userid from collection;
條件過濾 mongodb語(yǔ)法 : db.collection.find({"userid":495}) 對(duì)比sql語(yǔ)句:select * from collectionwhere userid = 495;
查詢?nèi)袷綍鴮懡忉?/strong> db.collection.find({},{}) 第一個(gè){}中,寫入的都是相當(dāng)于sql語(yǔ)句中where后的條件,多條件格式為{"key":value,"key2":"value2"} 第二個(gè){}中,寫入的都是相當(dāng)于sql語(yǔ)句中跟在select后邊的具體字段,格式為{"key":value,"key2":value} 當(dāng)value = 0時(shí)為不顯示此字段值,當(dāng)value !=0,即等于任何非0值時(shí),則為顯示此字段。 例: 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返回固定條目數(shù) 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返回結(jié)果集總數(shù) 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;
當(dāng)文檔類型為數(shù)組時(shí),使用$all進(jìn)行匹配,非數(shù)組類型使用時(shí)與單一匹配一樣。 查詢操作符"$size"-->用于數(shù)組查詢,查詢指定長(zhǎng)度的數(shù)組 mongodb查詢: db.collection.find({"remark":{"$size":"3"}})
查詢操作符"$in"--> 在范圍內(nèi) mongodb查詢: db.collection.find({"userid":{"$in":["297","495"]}}) sql查詢: select * from collection where userid in (297,495);
查詢操作符"$nin"-->不在范圍內(nèi) mongodb查詢: db.collection.find({"userid":{"$nin":["297","495"]}}) sql查詢: select * from collection where userid not in (297,495); 查詢操作符"$and"-->至少包含兩個(gè)表達(dá)式,兩個(gè)表達(dá)式都滿足的文檔返回 mongodb查詢: db.collection.find({"$and":[{"userid":"495"},{"type":"info"}]}) sql查詢: select * from collection where userid=495 and type='info';
查詢操作符"$nor"-->至少包含兩個(gè)表達(dá)式,兩個(gè)表達(dá)式都不滿足的文檔返回 mongodb查詢: db.collection.find({"$nor":[{"userid":"495"},{"userid":"297"}]}) sql查詢: select * from collection where userid not in (297,495);
查詢操作符"$not"-->找出不匹配表達(dá)式的文檔,不能夠單獨(dú)使用,必須與其他表達(dá)式配合使用 mongodb查詢: db.collection.find({"userid":{"$not":{"$gt":"297"}}}) 等同于:db.collection.find({"userid":{"$lte":"297"}}}) sql查詢: select * from collection where userid <=297; 查詢操作符"$or"-->至少包含兩個(gè)表達(dá)式,兩個(gè)表達(dá)式至少滿足一個(gè)的文檔返回 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"-->鍵值對(duì)變量參數(shù)取模,值等于另一個(gè)參數(shù) mongodb查詢: db.collection.find({"userid":{"$mod":[10,7]}}) 執(zhí)行條件:userid字段值必須是數(shù)字,userid對(duì)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正則寫法: select * from collection where userid regexp ".5$";
正則匹配userid的最后一位是5的,sql中只有使用regexp才可以使用復(fù)雜的正則表達(dá)式,使用Like的方式不可以進(jìn)行復(fù)雜的正則匹配
查詢操作符"$slice"-->控制返回的數(shù)組元素中的元素個(gè)數(shù) mongodb查詢: db.collection.find({},{"remark":{"$slice":5}) remark數(shù)組鍵值,返回?cái)?shù)組鍵值中的前5個(gè)元素 db.collection.find({},{"remark":{"$slice":[10,5]}) remark數(shù)組鍵值,返回?cái)?shù)組鍵值中的第11到15個(gè)元素,偏移量為10,然后返回5個(gè)。 db.collection.find({},{"remark":{"$slice":-5}) remark數(shù)組鍵值,返回?cái)?shù)組鍵值中的后5個(gè)元素
|
|