zoukankan      html  css  js  c++  java
  • 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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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" : "https://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为"https://www.cnblogs.com/lijiaman/p/12770415.html"且浏览量大于40或者是标题为"MySQL闪回工具--MyFlash"的文档。

    > db.blog.find(
     ...   { 
     ...     Link : "https://www.cnblogs.com/lijiaman/p/12770415.html",
     ...     $or : [{views:{$gt:40}},{title:"MySQL闪回工具--MyFlash"}]
     ...   }
     ... ).pretty()
     {
         "_id" : ObjectId("5ebe674278420c5d36520584"),
         "title" : "MySQL闪回工具--MyFlash",
         "Link" : "https://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='https://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" : "https://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" : "https://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" : "https://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" : "https://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
             }
         ]
     }



    【完】

  • 相关阅读:
    day22 Python shelve模块
    day22 Python pickle模块
    day22 Python sys模块
    day22 Python os模块
    day22 Python random模块
    day22 Python 时间模块
    day21 Python 实现的深度优先搜索实现迷宫算法
    day21 Go 实现的深度优先搜索实现迷宫算法
    杂篇
    杂篇
  • 原文地址:https://www.cnblogs.com/lijiaman/p/13159240.html
Copyright © 2011-2022 走看看