日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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

 Baruch 2018-04-10

工具推薦:Robomongo,可自行百度尋找下載源,個人比較推薦這個工具,相比較mongoVUE則更加靈活。

 

集合簡單查詢方法

mongodb語法:db.collection.find()  //collection就是集合的名稱,這個可以自己進行創(chuàng)建。

對比sql語句:select * from collection;

查詢集合中所有的文檔,即關(guān)系型數(shù)據(jù)庫中的查詢表中的所有數(shù)據(jù)。

 

返回制定的鍵值

mongodb語法:db.collection.find({},{"userid":1})

對比sql語句:select userid from collection;

 

條件過濾

mongodb語法 : db.collection.find({"userid":495})

對比sql語句:select * from collectionwhere userid = 495;

 

  

查詢?nèi)袷綍鴮懡忉?/strong>

  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返回固定條目數(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;

 

    當文檔類型為數(shù)組時,使用$all進行匹配,非數(shù)組類型使用時與單一匹配一樣。


查詢操作符"$size"-->用于數(shù)組查詢,查詢指定長度的數(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"-->至少包含兩個表達式,兩個表達式都滿足的文檔返回

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"-->鍵值對變量參數(shù)取模,值等于另一個參數(shù)

mongodb查詢:

        db.collection.find({"userid":{"$mod":[10,7]}})

        執(zhí)行條件:userid字段值必須是數(shù)字,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才可以使用復(fù)雜的正則表達式,使用Like的方式不可以進行復(fù)雜的正則匹配

 

查詢操作符"$slice"-->控制返回的數(shù)組元素中的元素個數(shù)

mongodb查詢:

        db.collection.find({},{"remark":{"$slice":5})

                    remark數(shù)組鍵值,返回數(shù)組鍵值中的前5個元素

        db.collection.find({},{"remark":{"$slice":[10,5]})

                    remark數(shù)組鍵值,返回數(shù)組鍵值中的第11到15個元素,偏移量為10,然后返回5個。

        db.collection.find({},{"remark":{"$slice":-5})

                    remark數(shù)組鍵值,返回數(shù)組鍵值中的后5個元素

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多