Mongodb操作之查詢(循序漸進對比SQL語句)

凌.風發表於2014-12-31

工具推薦: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正則寫法:      
     select * from collection where userid regexp ".5$";
 
   正則匹配userid的最後一位是5的,sql中只有使用regexp才可以使用複雜的正規表示式,使用Like的方式不可以進行復雜的正則匹配

 

查詢操作符"$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個元素

 

 逐漸補充中......

 

相關文章