學習MongoDB 四: MongoDB查詢(一)

來源:互聯網
上載者:User

標籤:oar   類型   comment   board   運算子   source   sso   dep   mongo   

原文:http://blog.csdn.net/congcong68/article/details/46841075

 

 

一、簡介

    MongoDB提供了db.collection.find() 方法可以實現根據條件查詢和指定使用投影運算子返回的欄位省略此參數返回匹配文檔中的所有欄位。

 

二.db.collection.find()查詢資料

   文法

   

[sql] view plain copy 
  1. > db.collection.find(query,projection)  

 

 

參數

類型

描述

query

document

可選. 使用查詢操作符指定查詢條件

projection

document

指定使用投影運算子返回的欄位省略此參數返回匹配文檔中的所有欄位

 

projection文法:

 

 

[sql] view plain copy 
  1. { field1: <boolean>, field2: <boolean> ... }  



 

 

 說明:

   1或者true表示返回欄位

   0或者false表示不返回該欄位

 

    _id:預設就是1,沒指定返回該欄位時,預設會返回,除非設定為0是,就不會返回該欄位。

    指定返回欄位,有時文檔欄位多並資料大時,我們指定返回我們需要的欄位,這樣既節省傳輸資料量,減少了記憶體消耗,提高了效能,在資料大時,效能很明顯的。

   

 

1.  查詢資料

 

   

(1)返回集合中所有文檔:

     db.collection.find()

     或者

    db.collection.find({})

 

   

 

   如同SQL語句:

   SELECT * FROM TABLENAME

 

 

   

   

 

 

 

(2)指定使用投影運算子返回的欄位省略此參數返回匹配文檔中的所有欄位

      

         文法:

         

[sql] view plain copy 
  1. db.orders.find({},{field1: <boolean>, field2: <boolean> ... })  

 

 

     例子:

   

[sql] view plain copy 
  1. >db.orders.find({},{"onumber":1,"cname":1})  

 

 

   而_id預設設定是1,所有也返回回來

 

    可以設定_id不返回

   

    例子:

 

 

[sql] view plain copy 
  1. >db.orders.find({},{"onumber":1,"cname":1,"_id":0})  


 

 

 

 

2. 根據條件查詢    (1)     等於條件查詢

           

     文法:

 

[sql] view plain copy 
  1. >db.collect.find({<field1>: <value1>,<field2>: <value2>, ... })  

 

 

 

    例子:

 

[sql] view plain copy 
  1. >db.orders.find({"onumber":"002"})  

 

 

 

     尋找onumber=002的文檔

 

  (2)     比較操作符

 

    $gt(大於)、$gte(大於或等於)、 $lt(小於)、 $lte(小於或等於)

 

    文法:

 

[sql] view plain copy 
  1. { <field1>: { <expression1> },<field2>: {<expression1> }, ... }  

 

 

 

     1)$gt(大於)比較操作符

 

       例子:

        

[sql] view plain copy 
  1. >db.orders.find({"onumber":{$gt:"003"}})  

        

 

       

          

        我們尋找onumber>003的值只有004

 

    2)$gte(大於或等於)與 $lte(小於或等於)聯集查詢並指定返回欄位(通過第二參數)

 

       例子:

 

[delphi] view plain copy 
  1. >db.orders.find({"onumber":{$gte:"002",$lte:"003"}},{"onumber":1,"cname":1})  

   

 

   

 

      我們尋找002=<onumber<=003 並指定返回onumber和cname欄位,而_id預設設定是1,所有也返回回來

 

(3) $or、和 $and 條件查詢

      1)$and 條件查詢

      

        文法:

           

[sql] view plain copy 
  1. { $and: [ { <expression1> }, { <expression2> } , ... , ]}  

 

 

      簡單的用法的文法:

     

[sql] view plain copy 
  1. { <field1>: <value1>,<field2>: <value2>, ...}  

 

 

     例子:

     

[sql] view plain copy 
  1. >db.orders.find({"onumber":"002","cname":"zcy2"})  

   

 

   

     尋找onumber=002 AND cname= zcy2 查詢文檔

     

   2)$or(或者)條件查詢

      

      文法:

      

[sql] view plain copy 
  1. { $nor: [ { <expression1> }, { <expression2> }, ... ] }  

 

 

    例子:

      

[sql] view plain copy 
  1. >db.orders.find({$or:[{"onumber":"002"},{"cname":"zcy1"}]})  

 

 

 

    我們條件onumber=002 OR cname= zcy1 尋找只要符合onumber=002或者cname= zcy1條件的文檔

 

  3) $or 和$and 聯合條件查詢

   

    例子:

   

 

[sql] view plain copy 
  1. >db.orders.find({$and:[{"date":"2015-07-01"},{$or:[{"onumber":"002"},{"cname":"zcy1"}]}]})  

  

 

     

     查詢條件 date=2015-07-01and (onumber=002 OR cname=zcy1) 的文檔,就是既等如date等於2015-07-01 並且要滿足onumber等於002或者cname等於zcy1其中一個就可以。

(4)$in(包含)、$nin(不包含)條件查詢

       1)$in(包含)條件查詢

 

           文法:

              

[sql] view plain copy 
  1. { field: { $in: [<value1>, < value2>, ...] } }  

 

 

          例子:

                

[sql] view plain copy 
  1. >db.orders.find({"onumber":{$in:["001","002"]}})  

 

 

               

             查詢onumber in("001","002") 條件的文檔,就是onumber等於001或者等於002 這個跟$or有點像,不過$or做為條件查詢時,可以指定不同的欄位:  db.orders.find({$or:[{"onumber":"002"},{"cname":"zcy1"}]})

,而$in只針對一個欄位。

 

       2)$nin(不包含)條件查詢

           

           文法:

               

[sql] view plain copy 
  1. { field: { $nin: [<value1>, < value2>, ...] } }  

 

            $nin(不包含)跟$in(包含)相反的,這裡就不做具體的介紹

 

(5)$not(不等於) 條件查詢

       文法:

        

[sql] view plain copy 
  1. { field: { $not: { < expression1> } } }  

 

 

        $not操作符不能獨立使用,必須跟其他動作條件一起使用(除$regex)

         

           

 

    例子:

        

[sql] view plain copy 
  1. >db.orders.find({"onumber":{$not:{$gt:"002"}}})  

 

 

         

      尋找onumber不等於大於002的文檔資料

 

(6)$exists用來判斷一個field是否存在

      文法:

          

[sql] view plain copy 
  1. { field: { $ exists:  < boolean>  } }  

 

 

       例子:

          

[sql] view plain copy 
  1. >db.orders.find({"age":{$exists:true}})  

 

 

 

     沒有age這個元素,什麼都沒返回

 

     插入有age元素,在執行一下

 

         

 

 

(7)$mod模數並等於指定的值

     文法:

       

[sql] view plain copy 
  1. { field: { $mod: [ value, value2 ]} }  

 

 

     對元素field值對value模數的,模數的值要value2的文檔資料

 

     例子:

        

[sql] view plain copy 
  1. >db.orders.find({"age":{$mod:[5,1]}})  

 

 

        

     對age元素的值和5模數,模數的值要等於1的文檔資料

 

(8)null 尋找元素不存在和元素對應的值為null的文檔

      文法:

         { field: null }

  

      例子:

       

[sql] view plain copy 
  1. >db.orders.find({"age":null})  

 

           

 

       1)      尋找age元素存在並值等於null

            

[sql] view plain copy 
  1. >db.orders.find({"age":{$in:[null],$exists:true}})  

 

              

  

(9) $type來匹配一個元素的類型

     文法:

       { field: { $type: < number >} }

 

      number 是MongoDB中使用的類型對應的類型值

類型

類型值

Double

1

String

2

Object

3

Array

4

Binary data

5

Undefined (deprecated)

6

Object id

7

Boolean

8

Date

9

Null

10

Regular Expression

11

JavaScript

13

Symbol

14

JavaScript (with scope)

15

32-bit integer

16

Timestamp

17

64-bit integer

18

Min key

255

Max key

127

學習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.