【轉】MongoDB學習筆記(查詢)

來源:互聯網
上載者:User

標籤:ast   結果   sql   mon   自增   類型   判斷   否則   log   

原文地址

MongoDB學習筆記(查詢)

基本查詢:

構造查詢資料。

> db.test.findOne(){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "[email protected]"}
  • 多條件查詢。下面的樣本等同於SQL語句的where name = "stephen" and age = 35

    > db.test.find({"name":"stephen","age":35}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35, "genda" : "male", "email" : "[email protected]" }
  • 返回指定的文檔索引值對。下面的樣本將只是返回name和age索引值對。

    > db.test.find({}, {"name":1,"age":1})   { "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35 }
  • 指定不返回的文檔索引值對。下面的樣本將返回除name之外的所有索引值對。

    > db.test.find({}, {"name":0}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "age" : 35, "genda" : "male", "email" : "[email protected]" }
查詢條件

MongoDB提供了一組比較操作符:$lt/$lte/$gt/$gte/$ne,依次等價於</<=/>/>=/!=

  • 下面的樣本返回符合條件age >= 18 && age <= 40的文檔。
> db.test.find({"age":{"$gte":18, "$lte":40}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" }
  • 下面的樣本返回條件符合name != "stephen1"

    > db.test.find({"name":{"$ne":"stephen1"}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" }
  • $in等同於SQL中的in,下面的樣本等同於SQL中的in ("stephen","stephen1")

> db.test.find({"name":{"$in":["stephen","stephen1"]}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" }  
  • 和SQL不同的是,MongoDB的in list中的資料可以是不同類型。這種情況可用於不同類型的別名情境。

    > db.test.find({"name":{"$in":["stephen",123]}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" } 
  • $nin等同於SQL中的not in,同時也是$in的取反。如:

    > db.test.find({"name":{"$nin":["stephen2","stephen1"]}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" }
  • $or等同於SQL中的or,$or所針對的條件被放到一個數組中,每個數組元素表示or的一個條件。
  • 下面的樣本等同於name = "stephen1" or age = 35

    > db.test.find({"$or": [{"name":"stephen1"}, {"age":35}]}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" } 
  • 下面的樣本示範了如何混合使用$or$in

    > db.test.find({"$or": [{"name":{"$in":["stephen","stephen1"]}}, {"age":36}]}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" } 
  • $not表示取反,等同於SQL中的not。

    > db.test.find({"name": {"$not": {"$in":["stephen2","stephen1"]}}}){ "_id" : ObjectId("4fd58ecbb9ac507e96276f1a"), "name" : "stephen", "age" : 35,"genda" : "male", "email" : "[email protected]" }
null資料類型的查詢
  • 在進行值為null資料的查詢時,所有值為null,以及不包含指定鍵的文檔均會被檢索出來。

    > db.test.find({"x":null}){ "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }{ "_id" : ObjectId("4fd59d49b9ac507e96276f1c"), "y" : 1 }
  • 需要將null作為數組中的一個元素進行相等性判斷,即便這個數組中只有一個元素。 再有就是通過$exists判斷指定鍵是否存在。

    > db.test.find({"x": {"$in": [null], "$exists":true}}){ "_id" : ObjectId("4fd59d30b9ac507e96276f1b"), "x" : null }
正則查詢
  • MongoDB中使用了Perl規則的正則文法。如:

    > db.test.find(){ "_id" : ObjectId("4fd59ed7b9ac507e96276f1d"), "name" : "stephen" }{ "_id" : ObjectId("4fd59edbb9ac507e96276f1e"), "name" : "stephen1" }

    --i表示忽略大小寫

    > db.test.find({"name":/stephen?/i}){ "_id" : ObjectId("4fd59ed7b9ac507e96276f1d"), "name" : "stephen" }{ "_id" : ObjectId("4fd59edbb9ac507e96276f1e"), "name" : "stephen1" } 
數組資料查詢
  • 基於數組的尋找。

    > db.test.find(){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }
  • 數組中所有包含banana的文檔都會被檢索出來。

    > db.test.find({"fruit":"banana"}){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] }
  • 檢索數組中需要包含多個元素的情況,這裡使用$all。下面的樣本中,數組中必須同時包含apple和banana,但是他們的順序無關緊要。

    > db.test.find({"fruit": {"$all": ["banana","apple"]}}){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple" ] } 
  • 下面的樣本表示精確匹配,即被檢索出來的文檔,fruit值中的數組資料必須和查詢條件完全符合,即不能多,也不能少,順序也必須保持一致。

    > db.test.find({"fruit":["apple","banana","peach"]}){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] } 
  • 下面的樣本將匹配數組中指定下標元素的值。數組的起始下標是0。

    > db.test.find({"fruit.2":"peach"}){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", peach" ] } 
  • 可以通過$size擷取數組的長度,但是$size不能和比較操作符聯合使用。

> db.test.find({"fruit": {$size : 3}}){ "_id" : ObjectId("4fd5a177b9ac507e96276f1f"), "fruit" : [ "apple", "banana", "peach" ] }{ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat","orange" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana","apple" ] } 
  • 如果需要檢索size > n的結果,不能直接使用$size,只能是添加一個額外的鍵表示資料中的元素資料,在操作資料中的元素時,需要同時更新size鍵的值。
  • 為後面的實驗構造資料。

    > db.test.update({}, {"$set": {"size":3}},false,true)> db.test.find(){ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat", "orange" ], "size" : 3 }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple" ], "size" : 3 } 
  • 每次添加一個新元素,都要原子性的自增size一次。

    > test.update({},{"$push": {"fruit":"strawberry"},"$inc":{"size":1}},false,true)> db.test.find(){ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat", "orange", "strawberry" ], "size" : 4 }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana", "apple", "strawberry" ], "size" : 4 }
  • 通過$slice返回數組中的部分資料。"$slice":2表示數組中的前兩個元素。

> db.test.find({},{"fruit": {"$slice":2}, "size":0}){ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "apple", "kumquat" ]}{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "cherry", "banana" ]} 
  • 通過$slice返回數組中的部分資料。"$slice":-2表示數組中的後兩個元素。

    > db.test.find({},{"fruit": {"$slice":-2}, "size":0}){ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "orange", "strawberry" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "apple", "strawberry" ] }

    -$slice : [2,1],表示從第二個2元素開始取1個,如果擷取數量大於2後面的元素數量,則取後面的全部資料。

    > db.test.find({},{"fruit": {"$slice":[2,1]}, "size":0}){ "_id" : ObjectId("4fd5a18cb9ac507e96276f20"), "fruit" : [ "orange" ] }{ "_id" : ObjectId("4fd5a1f0b9ac507e96276f21"), "fruit" : [ "apple" ] }
內嵌文檔查詢
  • 為後面的樣本構造測試資料。

    > db.test.find(){ "_id" : ObjectId("4fd5ada3b9ac507e96276f22"), "name" : { "first" : "Joe", "last" : "He" }, "age" : 45 }
  • 當嵌入式文檔為數組時,需要$elemMatch操作符來協助定位某一個元素匹配的情況,否則嵌入式檔案將進行全部的匹配。
  • 即檢索時需要將所有元素都列出來作為查詢條件方可。

    > db.test.findOne(){ "_id" : ObjectId("4fd5af76b9ac507e96276f23"), "comments" : [ { "author" : "joe", "score" : 3 }, { "author" : "mary", "score" : 6 } ]}> db.test.find({"comments": {"$elemMatch": {"author":"joe","score":{"$gte":3}}}}{ "_id" : ObjectId("4fd5af76b9ac507e96276f23"), "comments" : [ { "author" : "joe", "score" : 3 }, { "author" : "mary", "score" : 6 } ] }
遊標
  • 資料庫使用遊標來返回find()的執行結果,用戶端對遊標可以進行有效控制,如:限定結果集的數量、跳過部分結果、基於任意鍵的任意方向的排序等。 下面的例子將用於準備測試資料。

    > db.testtable.remove()> for (i = 0; i < 10; ++i) {... db.testtable.insert({x:i})... }

    我們可以通過cursor提供的hasNext()方法判斷是否還有未讀取的資料,再通過next()方法讀取結果集中的下一個文檔。如:

    > var c = db.testtable.find()> while (c.hasNext()) {... print(c.next().x)... }

    當調用find()的時候,shell並不立即查詢資料庫,而是等待真正開始要求獲得結果的時候才發送查詢,這樣在執行之前可以給查詢附加額外的選項。幾乎所有的遊標方法都返回本身,因此可以像下面這樣將遊標的方法鏈式組合起來。如:

    > var c1 = db.testtable.find().sort({"x":1}).limit(1).skip(4);> var c2 = db.testtable.find().limit(1).sort({"x":1}).skip(4);> var c3 = db.testtable.find().skip(4).limit(1).sort({"x":1});

    此時,查詢並未執行,所有這些函數都是在構造查詢,當執行下面的語句時,查詢將被真正執行,

    > c.hasNext()

    查詢被發送到伺服器,MongoDB伺服器每次將返回一批資料,當本批被全部迭代後再從伺服器讀取下一批資料,直至查詢結果需要的資料被全部迭代。

對於上面的樣本,limit(1)表示輸出結果僅為一個,如果小於1,則不輸出,即limit(n)函數限定的是最多輸出結果。skip(4)表示跳過查詢結果中的前4個文檔,如果結果小於4,則不會返回任何文檔。sort({"x":1})用於設定排序條件,即按照x鍵以升序(1)的方式排序,如果需要降序排序可以改為:sort({"x":-1})。sort也可以支援多鍵排序,如:sort({username:1, age:-1})即先按照username進行升序排序,如果username的值相同,再以age鍵進行降序排序。這裡需要指出的是,如果skip過多的文檔,將會導致效能問題。

【轉】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.