MongoDB文檔(二)–查詢

clipboard

(一)查詢文檔
查詢文檔可以使用以下方法

# 以非結構化的方式顯示所有的文檔
db.<collectionName>.find(document)

# 以結構化的方式顯示所有文檔
db.<collectionName>.find(document).pretty()

# 只返回一個文檔(結構化方式)
db.<collectionName>.findOne()

測試1 : 使用find()方法以非結構化的方式查詢文檔

> db.blog.find()
 { "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"), "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理", "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html", "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 23:17", "views" : 57, "comments" : [ { "user" : "user1", "message" : "mark!", "like" : 0 } ] }
 { "_id" : ObjectId("5ebd71b4c50e24a9d8fb2a7b"), "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理", "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html", "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 23:17", "views" : 57, "comments" : [ { "user" : "user1", "message" : "mark!", "like" : 0 } ] }
 { "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"), "title" : "如何為Linux伺服器添加磁碟", "Link" : "//www.cnblogs.com/lijiaman/p/12885028.html", "summary" : "Linux伺服器如果磁碟不夠用了,就需要增加新的磁碟,磁碟添加到使用通常有4個步驟...", "tags" : [ "Linux", "study" ], "post" : "2020-05-13 21:31", "views" : 25, "comments" : "" }
 { "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"), "title" : "MySQL閃回工具--MyFlash", "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html", "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...", "tags" : [ "mysql", "study" ], "post" : "2020-04-24 21:38", "views" : 23, "comments" : "" }
 >

測試2:使用pretty()方法以結構化的方式查詢文檔

> db.blog.find().pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 {
     "_id" : ObjectId("5ebd71b4c50e24a9d8fb2a7b"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
     "title" : "如何為Linux伺服器添加磁碟",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885028.html",
     "summary" : "Linux伺服器如果磁碟不夠用了,就需要增加新的磁碟,磁碟添加到使用通常有4個步驟...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 21:31",
     "views" : 25,
     "comments" : ""
 }
 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"),
    "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

測試3: 使用findOne()方法返回一個結構化文檔

> db.blog.findOne()
{
    "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
    "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
    "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
    "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
    "tags" : [
        "Linux",
        "study"
    ],
    "post" : "2020-05-13 23:17",
    "views" : 57,
    "comments" : [
        {
            "user" : "user1",
            "message" : "mark!",
            "like" : 0
        }
    ]
}
 >

(二)MongoDB與RDBMS等效的where子句


操作 語法 例子 RDBMS等效例子
相等(=) {<key>:<value>} db.blog.find({title:”MySQL閃回工具–MyFlash”}) where title=”MySQL閃回工具–MyFlash”
大於(>) {<key>:{$gt:<value>}} db.blog.find({views:{$gt:40}}) where views > 40
大於等於(>=) {<key>:{$gte:<value>}} db.blog.find({views:{$gte:57}}) where views>=57
小於(<) {<key>:{$lt:<value>}} db.blog.find({views:{$lt:25}}) where views<25
小於等於(<=) {<key>:{$lte:<value>}} db.blog.find({views:{$lte:25}}) where views<=25
不等於(<>) {<key>:{$ne:<value>}} db.blog.find({views:{$ne:25}}) where views!=25

例子1:查看blog集合中標題為「MySQL閃回工具–MyFlash」的文檔

> db.blog.find({title:"MySQL閃回工具--MyFlash"}).pretty()
 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7d"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

例子2 :查看blog集合中瀏覽次數大於40的文檔

> db.blog.find({views:{$gt:40}}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 >

例子3 :查看blog集合中瀏覽次數大於等於57次的文檔

> db.blog.find({views:{$gte:57}}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 >

例子4:查詢blog集合中瀏覽次數小於25次的文檔

> db.blog.find({views:{$lt:25}}).pretty()
 {
     "_id" : ObjectId("5ebe674278420c5d36520584"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

例子5:查詢blog集合中瀏覽此時小於等於25次的文檔

> db.blog.find({views:{$lte:25}}).pretty()
 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
     "title" : "如何為Linux伺服器添加磁碟",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885028.html",
     "summary" : "Linux伺服器如果磁碟不夠用了,就需要增加新的磁碟,磁碟添加到使用通常有4個步驟...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 21:31",
     "views" : 25,
     "comments" : ""
 }
 {
     "_id" : ObjectId("5ebe674278420c5d36520584"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

例子6:查詢blog集合中瀏覽次數不等於25次的文檔

> db.blog.find({views:{$ne:25}}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 {
     "_id" : ObjectId("5ebe674278420c5d36520584"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

(三)MongoDB中的AND操作
AND的語法:

db.<collectionName>.find(
     {
         $and:[
             {key1:value1},{key2:value2}
         ]
     }
 ).pretty()

例子 :查詢blog集合中標題為「如何為Linux伺服器添加磁碟」並且瀏覽次數大於20次的文檔

> db.blog.find(
 ...   {
 ...       $and : [
 ...         {title:"如何為Linux伺服器添加磁碟"},
 ...         {views:{$gt:20}}
 ...       ]
 ...   }
 ... ).pretty()


 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
     "title" : "如何為Linux伺服器添加磁碟",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885028.html",
     "summary" : "Linux伺服器如果磁碟不夠用了,就需要增加新的磁碟,磁碟添加到使用通常有4個步驟...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 21:31",
     "views" : 25,
     "comments" : ""
 }
 >

上面的查詢相當於RDBMS中的:

select * from blog where title="如何為Linux伺服器添加磁碟" and views>20;

(四)MongoDB中的or操作
OR的語法:

db.<collectionName>.find(
     {
       $or:[
         {<key1>:<value1>},{<key2>:<value2>}  
       ]
     }
).pretty()

例子:查詢blog集合中訪問量在40以上或者是標題為「MySQL閃回工具–MyFlash」的文檔

> db.blog.find(
 ...   {
 ...     $or : [
 ...       {views:{$gt:40}},
 ...       {title:"MySQL閃回工具--MyFlash"}
 ...     ]
 ...   }
 ... ).pretty()


 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 {
     "_id" : ObjectId("5ebe674278420c5d36520584"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

上面的查詢相當於RDBMS中的:

select * from blog where views>40 or title="MySQL閃回工具--MyFlash"

(五)MongoDB中的AND和OR結合在一起
例子:查看bolg集合中Link為”//www.cnblogs.com/lijiaman/p/12770415.html”且瀏覽量大於40或者是標題為”MySQL閃回工具–MyFlash”的文檔。

> db.blog.find(
 ...   { 
 ...     Link : "//www.cnblogs.com/lijiaman/p/12770415.html",
 ...     $or : [{views:{$gt:40}},{title:"MySQL閃回工具--MyFlash"}]
 ...   }
 ... ).pretty()
 {
     "_id" : ObjectId("5ebe674278420c5d36520584"),
     "title" : "MySQL閃回工具--MyFlash",
     "Link" : "//www.cnblogs.com/lijiaman/p/12770415.html",
     "summary" : "MyFlash介紹 MyFlash是美團開發的一個回滾DML操作的工具,該工具是開源的...",
     "tags" : [
         "mysql",
         "study"
     ],
     "post" : "2020-04-24 21:38",
     "views" : 23,
     "comments" : ""
 }
 >

上面的查詢相當於RDBMS中的:

select * 
from   blog 
where  link='//www.cnblogs.com/lijiaman/p/12770415.html'
 and    (views>40 or title='MySQL閃回工具--MyFlash')

(六)MongoDB中的嵌套查詢
對於文檔裡面還包含文檔的情況,可以使用嵌套查詢,查詢內部文檔資訊。

(6.1)匹配嵌套文檔

例子:查詢blog集合中comments欄位等於{ “user” : “user1”, “message” : “mark!”, “like” : 0 }的文檔

> db.blog.find({comments:{ "user" : "user1", "message" : "mark!", "like" : 0 }}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 >

(6.2)查詢嵌套欄位
父欄位與子欄位之間用「.」隔開
例子:查詢blog表中comments欄位中嵌套的欄位user等於「user1」的文檔

> db.blog.find({"comments.user":"user1"}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }
 >

(6.3)嵌套查詢指定AND條件
查詢comments欄位中user欄位為「user1」,comments欄位中like欄位為0,views欄位為57的文檔。

> db.blog.find({"comments.user":"user1" , "comments.like":0,views:57}).pretty()
 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }

或者可以直接使用$and來查詢

> db.blog.find({
 ...     $and : [
 ...       {"comments.user":"user1"},
 ...       {"comments.like":0},
 ...       {views:57}
 ...     ]
 ... }).pretty()


 {
     "_id" : ObjectId("5ebd7133c50e24a9d8fb2a7a"),
     "title" : "Linux 物理卷(PV)、邏輯卷(LV)、卷組(VG)管理",
     "Link" : "//www.cnblogs.com/lijiaman/p/12885649.html",
     "summary" : "(一)相關概念邏輯卷是使用邏輯卷組管理(Logic Volume Manager)創建出來的設備,如果要了解邏輯卷,那麼首先...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 23:17",
     "views" : 57,
     "comments" : [
         {
             "user" : "user1",
             "message" : "mark!",
             "like" : 0
         }
     ]
 }

【完】