MongoDB常用的Query操作有哪些-創(chuàng)新互聯(lián)

這篇文章主要介紹MongoDB常用的Query操作有哪些,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)懷安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

                              本篇文章給大家?guī)淼膬?nèi)容是關(guān)于MongoDB的常用Query操作的介紹(附代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。

前言:使用的可視化工具是 Studio 3T,官網(wǎng)-->https://studio3t.com/
版本號(hào):MongoDB shell version v3.4.2
如何使用:https://blog.csdn.net/weixin_...
看點(diǎn):重點(diǎn)看操作符那塊。
如何查找:在此頁面按 ctrl+F 輸入關(guān)鍵字查找

一、常用Query
為方便操作,在插入原數(shù)據(jù)前,先刪除所有文檔(在項(xiàng)目中請(qǐng)謹(jǐn)慎操作!):

db.getCollection("inventory").deleteMany({})

0、查看所有文檔

db.getCollection("inventory").find({})

1、對(duì)象查找
1.1、原數(shù)據(jù)

db.inventory.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

1.2、查找 size.h 等于 14,size.w 等于 21,size.uom 等于 cm 的文檔

db.inventory.find( { size: { h: 14, w: 21, uom: "cm" } } )

1.3、查找 size.uom 等于 in 的文檔

db.inventory.find( { "size.uom": "in" } )

注意:當(dāng)查找單個(gè)對(duì)象屬性時(shí),務(wù)必加上引號(hào)!

1.4、查找并返回對(duì)象里的指定字段

db.inventory.find(
   { status: "A" },
   { item: 1, status: 1, "size.uom": 1 }
)

1.5、查找并過濾對(duì)象里的指定字段

db.inventory.find(
   { status: "A" },
   { "size.uom": 0 }
)

2、數(shù)組查找
2.1、原數(shù)據(jù)

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

2.2、查找 tags=["red", "blank"] 的文檔

db.inventory.find( { tags: ["red", "blank"] } )

注意:不是包含關(guān)系,即 tags: ["red", "blank", "plain"] 是不包括在內(nèi)的

2.3、查找 tags 包含 red 的文檔

db.inventory.find( { tags: "red" } )

注意:不能這么寫 db.inventory.find( { tags: ["red"] } ),這樣就表示查找 tags 是 red 的文檔

3、數(shù)組中包含對(duì)象的查找
3.1、原數(shù)據(jù)

db.inventory.insertMany( [
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

3.2、查找數(shù)組中有一個(gè)對(duì)象符合條件的(不是包含),只要數(shù)組中有一個(gè)對(duì)象符合條件就返回整個(gè)數(shù)組

db.inventory.find( { "instock": { warehouse: "A", qty: 5 } } )

要嚴(yán)格按照字段的順序來,如果調(diào)換字段順序會(huì) 找 不 到,如下:

db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )

3.3、查找數(shù)組中的元素對(duì)象,有一個(gè)元素對(duì)象的qty=5,或者該對(duì)象(或者是其他元素對(duì)象)的warehouse=A

db.inventory.find( { "instock.qty": 5, "instock.warehouse": "A" } )

3.4、查找數(shù)組中的對(duì)象,并返回對(duì)象的某個(gè)屬性

db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )

4、普通查找
4.1、原數(shù)據(jù)

db.inventory.insertMany( [
  { item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] },
  { item: "notebook", status: "A",  size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] },
  { item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] },
  { item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] },
  { item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

4.2、查詢并返回指定字段
在 status=A 的條件下,返回 _id,item,status 字段

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

結(jié)果:

{ "_id" : ObjectId("5c91cd53e98d5972748780e1"), 
    "item" : "journal", 
    "status" : "A"}
// ----------------------------------------------
{ "_id" : ObjectId("5c91cd53e98d5972748780e2"), 
    "item" : "notebook", 
    "status" : "A"}
// ----------------------------------------------
{ "_id" : ObjectId("5c91cd53e98d5972748780e5"), 
    "item" : "postcard", 
    "status" : "A"}

4.3、由 4.2 可知,_id 是自動(dòng)帶著的,可以去掉,如下
查詢不帶(去掉) id :

db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )

注意:除了 id 可以在過濾掉的同時(shí),還去保留其他字段外,其他字段不能在 0 的同時(shí),還寫 1
如:

db.inventory.find( { status: "A" }, { item: 1, status: 0 } )

會(huì)報(bào)錯(cuò)

MongoDB常用的Query操作有哪些

4.4、排除特定字段,返回其他字段

db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )

5、查找 null 或不存在的 鍵
5.1、原數(shù)據(jù)

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])

5.2、查找 item 為 null 的文檔,或者不包含 item 的文檔

db.inventory.find( { item: null } )

二、操作符
1、$lt  less than 小于
1.1、原數(shù)據(jù)

db.inventory.insertMany( [
   { item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
   { item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
   { item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
   { item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
   { item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);

1.2、查找 "size.h" 小于 15 的文檔集合

db.inventory.find( { "size.h": { $lt: 15 } } )

1.3、$lt 與 AND 聯(lián)用
查找 size.h 小于 15,并且 size.uom 是 in ,并且 status 是 D 的文檔

db.inventory.find( { "size.h": { $lt: 15 }, "size.uom": "in", status: "D" } )

2、$lte less than equal 小于等于
2.1、原數(shù)據(jù)

db.inventory.insertMany( [
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

2.2、查找 instock.qty 小于等于 20 的文檔,只要數(shù)組中有一個(gè)對(duì)象符合條件就返回整個(gè)數(shù)組

db.inventory.find( { 'instock.qty': { $lte: 20 } } )

3、$gt greater than 大于
3.1、原數(shù)據(jù)

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

3.2、查找 dim_cm 大于 25 的文檔

db.inventory.find( { dim_cm: { $gt: 25 } } )

注意:只要包含大于 25 的元素的數(shù)組,都是符合條件的

3.3、查找 dim_cm 大于 15,或小于 20,或既大于 15,又小于 20 的文檔

db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

3.4、查找 dim_cm 既大于 22,又小于 30 的文檔(是判斷數(shù)組的某一個(gè)元素是否是大于22,且小于30的,而不是判斷數(shù)組的所有元素)

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

3.5、根據(jù)數(shù)組位置查找
查找 dim_cm 的第二個(gè)元素 大于 25 的文檔

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

4、$size 根據(jù)數(shù)組長度查找
查找 tags 長度是 3 的文檔

db.inventory.find( { "tags": { $size: 3 } } )

5、$gte 大于等于
5.1、原數(shù)據(jù)

db.inventory.insertMany( [
   { item: "journal", instock: [ { warehouse: "A", qty: 5 }, { warehouse: "C", qty: 15 } ] },
   { item: "notebook", instock: [ { warehouse: "C", qty: 5 } ] },
   { item: "paper", instock: [ { warehouse: "A", qty: 60 }, { warehouse: "B", qty: 15 } ] },
   { item: "planner", instock: [ { warehouse: "A", qty: 40 }, { warehouse: "B", qty: 5 } ] },
   { item: "postcard", instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }
]);

5.2、查找數(shù)組的第一個(gè)元素(對(duì)象)的qty 大于等于 20 的文檔集合

db.inventory.find( { 'instock.0.qty': { $gte: 20 } } )

6、$elemMatch 對(duì)象的屬性匹配
6.1、在數(shù)組中查找符合 qty=5, warehouse="A" 的對(duì)象,并返回該文檔集合

db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )

6.2、在數(shù)組中查找符合 qty 大于 10 并且小于等于 20 的文檔集合

db.inventory.find( { "instock": { $elemMatch: { qty: { $gt: 10, $lte: 20 } } } } )

如果不使用 $elemMatch 的話,就表示 qty 大于 10 或者小于等于 20,官方文檔意思是,不在數(shù)組的某一個(gè)元素找 既滿足條件 A 又滿足條件 B 的 qty,而是在數(shù)組的所有元素上找,滿足條件 A 或滿足條件 B 的 qty

db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } )

7、$slice 返回?cái)?shù)組特定位置的元素
7.1、原數(shù)據(jù)

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

7.2、查找并返回 tags 數(shù)組的最后一個(gè)元素

db.inventory.find( { item: "journal" }, { item: 1, qty: 0, tags: { $slice: -1 } } )

結(jié)果:

{ 
    "_id" : ObjectId("5c91dce5e98d5972748780e6"), 
    "item" : "journal", 
    "tags" : [
        "red"
    ]
}

8、$type 返回指定類型的元素
8.1、原數(shù)據(jù)

db.inventory.insertMany([
   { _id: 1, item: null },
   { _id: 2 }
])

8.2、返回 null 類型的數(shù)據(jù)

db.inventory.find( { item : { $type: 10 } } )

類型如下:

MongoDB常用的Query操作有哪些

詳細(xì)文檔請(qǐng)看:https://docs.mongodb.com/manu...

9、$exists 返回存在/不存在的鍵
查找不存在 item 鍵的數(shù)據(jù)

db.inventory.find( { item : { $exists: false } } )

10、$all 包含
10.1、原數(shù)據(jù)

db.inventory.insertMany([
   { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },
   { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },
   { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },
   { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },
   { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }
]);

10.2、查找 tags 數(shù)組包含 ["red", "blank"] 的文檔

db.inventory.find( { tags: { $all: ["red", "blank"] } } )

綜上:
數(shù)組用的:$all、$size、$slice
對(duì)象用的:$elemMatch

以上是“MongoDB常用的Query操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文標(biāo)題:MongoDB常用的Query操作有哪些-創(chuàng)新互聯(lián)
URL鏈接:http://bm7419.com/article18/dgoogp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)域名注冊(cè)、App設(shè)計(jì)服務(wù)器托管搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)