學習MongoDB 五: MongoDB查詢(數組、內嵌文檔)(二)

來源:互聯網
上載者:User

標籤:mongodb   nosql   


一、簡介        我們上一篇介紹了db.collection.find()可以實現根據條件查詢和指定使用投影運算子返回的欄位省略此參數返回匹配文檔中的所有欄位,我們今天介紹了對數組和內嵌文檔的查詢操作,尤其是對 $elemMatch 同樣可以用在find方法的第二個參數來限制返回數組內的元素,只返回我們需要的文檔的介紹。 我們經常在查詢條件查詢內嵌文檔數組時,只需要返回主文件並返回內嵌文檔數組中我們只需要的值,而不是把內嵌文檔的數組都返回。
二、對數組根據條件查詢

 

   $all、$size、$slice、$elemMatch

  

(1)$all尋找數組中包含指定的值的文檔

     文法:

        { field:{ $all: [ <value> , <value1> ... ]}

    

     

   例子:

     db.orders.find({"books":{$all:["java","mongo"]}})

   

      

    

     尋找books包含java、mongo的文檔資料

 

(2)$size 尋找數組大小等於指定值的文檔

     文法:

        

<span style="font-size:18px;">    {field: {$size: number } }</span>


      例子:

        

<span style="font-size:18px;">    db.orders.find({"books":{$size:2}})</span>

       

 

(3)$slice查詢數組中指定返回元素的個數

      文法:

         

<span style="font-size:18px;">         db.collect.find({},{field:{$slice: number }})</span>

 

      number 說明:

         為正數表示返回前面指定的值的個數:例如1 返回數組第一個

         為負數表示返回倒數指定的值的個數:例如-1返回數組倒數第一個

 

      例子:

            

<span style="font-size:18px;">       db.orders.find({"onumber":{$in:["008","009"]}},{books:{$slice:1}})</span>

          

 

 

    1)$slice可以查詢數組中第幾個到第幾個

    

        文法:

           

<span style="font-size:18px;">        db.collect.find({},{field:{$slice:[ number1, number2] }})</span>


       跳過數組的number1個位置然後返回number2個數

       number1說明:

          為正數表示跳到指定值的數組個數:例如2 跳到數組第3個

          為負數表示跳到指定值的數組倒數個數:例如-2跳到到數組倒數第3個

 

       例子:

          

<span style="font-size:18px;">        db.orders.find({"onumber":{$in:["008","009"]}},{books:{$slice:[1,1]}})</span>


         


         跳過books數組第一個元素,現在到數組第二個元素,並返回1個元素

 

 

三、對數組內嵌文檔查詢

      我們先儲存資料

<span style="font-size:18px;"> db. orders.insert([{        "onumber" : "001",         "date" : "2015-07-02",         "cname" : "zcy1",          "items" :[ {                   "ino" : "001",                  "quantity" :2,                   "price" : 4.0                 },{                   "ino" : "002",                  "quantity" : 4,                   "price" : 6.0                }                ]},{         "onumber" : "002",         "date" : "2015-07-02",         "cname" : "zcy2",          "items" :[ {                  "ino" : "001",                  "quantity" :2,                   "price" : 4.0                   },{                  "ino" : "002",                  "quantity" :6,                   "price" : 6.0                 }               ]}])</span>


 (1)$elemMatch 文檔包含有一個元素是數組,那麼$elemMatch可以匹配內數組內的元素並返迴文檔資料

     文法:

        

<span style="font-size:18px;">       {field:{$elemMatch:{ field1:value1, field2:value2,………}}}</span>

 

   例子:

       

<span style="font-size:18px;">    db.orders.find({"items":{$elemMatch:{"quantity":2}}})</span>

      

      返回quantity為2的文檔

 

   也可以這樣查詢db.orders.find({"items.quantity":2})

      

 

(2) $elemMatch可以帶多個查詢條件

   

   例子:

        

<span style="font-size:18px;">   db.orders.find({"items":{$elemMatch:{"quantity":4,"ino":"002"}}}) </span>

      

 

      我們查詢數組中的quantity等於4並且ino等於002,但是我們就想返回數組中的quantity等於4並且ino等於002的這個文檔,並不想把ino等於001等這些無關的文檔返回。

 

 

(3)$elemMatch 同樣可以用在find方法的第二個參數來限制返回數組內的元素,只返回我們需要的文檔

   

   例子:

         db.orders.find({"onumber":"001"},{"items":{$elemMatch:{"quantity":4,"ino":"002"}},"cname":1,"date":1,"onumber":1})

         

         

       

 我們只返回quantity等於4並且ino等於002的文檔,無關的文檔沒有返回,方便我們處理資料,這樣也可以節省傳輸資料量,減少了記憶體消耗,提高了效能,在資料大時,效能很明顯的。




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

學習MongoDB 五: MongoDB查詢(數組、內嵌文檔)(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.