• MongoDB学习笔记:文档Crud Shell


    原文:https://blog.csdn.net/leshami/article/details/52901240

    文档插入

    一、插入语法

    db.collection.insertOne() 将单个文档插入到集合中。
    db.collection.insertMany() 将多个文件插入集合中。

    文档删除

    一、删除语法

    db.collection.deleteOne() 即使多个文档可以匹配指定的过滤器,也最多删除匹配指定过滤器的单个文档。
    db.collection.deleteMany() 删除匹配指定过滤器的所有文档。

    文档查询

    一、查询语法

    db.collection.find( <query filter>, <projection> )
    db.collection.findOne() //仅仅返回单个文档,相当于使用limit

    <query filter> 查询的过滤条件
    <projection> 投影,即哪些列需要返回
    对于查询的结果可以添加limits, skips, sort 等方式控制返回的结果集
    缺省情况下,在mongo shell中对于未使用将结果集返回给变量的情形下,仅返回前20条记录
    注:本文描述中有些地方使用到了文档的键值对,称为键和值,有些地方称为列,是一个概念。

    二、准备数据

    //演示环境
        db.version()  
        3.2.9
    
        //插入演示数据
        db.users.insertMany(
          [
             {
               _id: 1,
               name: "sue",
               age: 19,
               type: 1,
               status: "P",
               favorites: { artist: "Picasso", food: "pizza" },
               finished: [ 17, 3 ],
               badges: [ "blue", "black" ],
               points: [
                  { points: 85, bonus: 20 },
                  { points: 85, bonus: 10 }
               ]
             },
             {
               _id: 2,
               name: "bob",
               age: 42,
               type: 1,
               status: "A",
               favorites: { artist: "Miro", food: "meringue" },
               finished: [ 11, 25 ],
               badges: [ "green" ],
               points: [
                  { points: 85, bonus: 20 },
                  { points: 64, bonus: 12 }
               ]
             },
             {
               _id: 3,
               name: "ahn",
               age: 22,
               type: 2,
               status: "A",
               favorites: { artist: "Cassatt", food: "cake" },
               finished: [ 6 ],
               badges: [ "blue", "red" ],
               points: [
                  { points: 81, bonus: 8 },
                  { points: 55, bonus: 20 }
               ]
             },
             {
               _id: 4,
               name: "xi",
               age: 34,         //Author : Leshami
               type: 2,         //Blog   : http://blog.csdn.net/leshami
               status: "D",
               favorites: { artist: "Chagall", food: "chocolate" },
               finished: [ 5, 11 ],
               badges: [ "red", "black" ],
               points: [
                  { points: 53, bonus: 15 },
                  { points: 51, bonus: 15 }
               ]
             },
             {
               _id: 5,
               name: "xyz",
               age: 23,
               type: 2,
               status: "D",
               favorites: { artist: "Noguchi", food: "nougat" },
               finished: [ 14, 6 ],
               badges: [ "orange" ],
               points: [
                  { points: 71, bonus: 20 }
               ]
             },
             {
               _id: 6,
               name: "abc",
               age: 43,
               type: 1,
               status: "A",
               favorites: { food: "pizza", artist: "Picasso" },
               finished: [ 18, 12 ],
               badges: [ "black", "blue" ],
               points: [
                  { points: 78, bonus: 8 },
                  { points: 57, bonus: 7 }
               ]
             }
          ]
        )

    三、演示查询

    1.简单查询

     //查询所有文档,文档太多,此处及以下演示查询结果省略
        db.users.find( {} )     //与方式等价于db.users.find()
        db.users.findOne( {} )  //查询单条记录
    
        //等值查询,{ <field1: <value1, ... }
        db.users.find({_id:5}).pretty()
        db.users.find({age:19,status:"P"})     //多条件等值查询,隐式使用$and运算符
    

    2.基于运算符的查询

    更多运算符:https://docs.mongodb.com/manual/reference/operator/query/

    //基于运算符的查询,{ <field1: { <operator1: <value1 }, ... }
        //基于$in运算符
        db.users.find( { status: { $in: [ "P", "D" ] } } )   
    
        //基于$and运算符的查询
        db.users.find( {$and: [{ status: "A", age: { $lt: 30 } } ]})
        db.users.find( { status: "A", age: { $lt: 30 } } ) //此查询方法与上一条等价,隐式使用$and运算符
    
        //基于$or运算符的查询
        db.users.find( { $or: [ { status: "A" }, { age: { $lt: 30 } }]})
    
        //多条件组合查询,基于$lt,也有$or,还有隐式$and
        db.users.find(
           {
             status: "A",
             $or: [ { age: { $lt: 30 } }, { type: 1 } ]
           }
        )

    3、内嵌文档查询

    //等值匹配内嵌文档
        db.users.find( { favorites: { artist: "Picasso", food: "pizza" } } )
    
        //等值匹配内嵌文档的特定键值,通过"键.成员名:值"的方式来进行
        db.users.find( { "favorites.artist": "Picasso" } )
    

    4、数组查询

     //查询数组元素            //查询数组badges中包含black的文档 
        db.users.find( { badges: "black" } ) 
    
        //匹配一个特定的数组元素  //查询数组badges中第一个元素为black的文档
        db.users.find( { "badges.0": "black" } )  //此处0表示数组的下标
    
        //匹配单个数组元素满足条件  //查询数组finished至少有一个元素的值大于15且小于20的文档
        db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )
    
        //匹配混合数组元素满足条件  //查询数组finished中任意的一个元素大于15,且另外一个元素小于20
        db.users.find( { finished: { $gt: 15, $lt: 20 } } )  //或者这个元素既大于15又小于20的文档
    
        //查询数组内嵌文档          //查询数组points元素1内嵌文档键points的值小于等于55的文档
        db.users.find( { 'points.0.points': { $lte: 55 } } )
    
        //查询数组内嵌文档         //查询数组points内嵌文档键points的值小于等于55的文档,此处未指定数组下标
        db.users.find( { 'points.points': { $lte: 55 } } )
    
        //查询数组元素至少一个内嵌文档满足所有条件的文档
        //如下,数组points内至少一个文档points键的值小于等于70,bonus键的值等于20的记录,这样的文档被返回
        db.users.find( { points: { $elemMatch: { points: { $lte: 70 }, bonus: 20 } } } )
    
        //查询数组元素任意一个内嵌文档满足所有条件的文档
        //如下,数组points内嵌文档任意一个文档points的值小于等于70,且数组内另外一个文档bonus值等于20
        //或者数组内某个内嵌文档points的值小于等于70,bonus的值等于20,这2种情形会被返回
        db.users.find( { "points.points": { $lte: 70 }, "points.bonus": 20 } )

    四、限制查询返回的结果集

     { field1: <value>, field2: <value> ... }
        1 or true  显示该字段
        0 or false 不显示该字段
    

    1、限制返回的列

    //查询结果中显示字段name及status,缺省情况下,文档的_id列会被返回
        > db.users.find( { status: "A" }, { name: 1, status: 1 } )
        { "_id" : 2, "name" : "bob", "status" : "A" }
        { "_id" : 3, "name" : "ahn", "status" : "A" }
        { "_id" : 6, "name" : "abc", "status" : "A" }
    
        //查询结果中显示字段name及status,且不显示_id列
        > db.users.find( { status: "A" }, { name: 1, status: 1, _id: 0 } )
        { "name" : "bob", "status" : "A" }
        { "name" : "ahn", "status" : "A" }
        { "name" : "abc", "status" : "A" }
    
        //返回查询中未列出的全部列名
        > db.users.find( { status: "A" }, { favorites: 0, points: 0 ,badges:0})
        { "_id" : 2, "name" : "bob", "age" : 42, "type" : 1, "status" : "A", "finished" : [ 11, 25 ] }
        { "_id" : 3, "name" : "ahn", "age" : 22, "type" : 2, "status" : "A", "finished" : [ 6 ] }
        { "_id" : 6, "name" : "abc", "age" : 43, "type" : 1, "status" : "A", "finished" : [ 18, 12 ] }
    
        //返回内嵌文档指定的列名,相反地,如果不显示内嵌文档的某个列,将在置0即可
        > db.users.find(
               { status: "A" },
               { name: 1, status: 1, "favorites.food": 1 }
            )
        { "_id" : 2, "name" : "bob", "status" : "A", "favorites" : { "food" : "meringue" } }
        { "_id" : 3, "name" : "ahn", "status" : "A", "favorites" : { "food" : "cake" } }
        { "_id" : 6, "name" : "abc", "status" : "A", "favorites" : { "food" : "pizza" } }
    
        //返回数组内内嵌文档的指定列,如下查询为数组points内嵌文档bonus列
        > db.users.find( { status: "A" },{ name: 1,"points.bonus": 1 } )
        { "_id" : 2, "name" : "bob", "points" : [ { "bonus" : 20 }, { "bonus" : 12 } ] }
        { "_id" : 3, "name" : "ahn", "points" : [ { "bonus" : 8 }, { "bonus" : 20 } ] }
        { "_id" : 6, "name" : "abc", "points" : [ { "bonus" : 8 }, { "bonus" : 7 } ] }
    
        //下面的查询使用了$slice操作符,这将仅仅返回符合status为A,且显示数组中的最后一个元素
        > db.users.find( { status: "A" }, { name: 1, status: 1, points: { $slice: -1 } } )
        { "_id" : 2, "name" : "bob", "status" : "A", "points" : [ { "points" : 64, "bonus" : 12 } ] }
        { "_id" : 3, "name" : "ahn", "status" : "A", "points" : [ { "points" : 55, "bonus" : 20 } ] }
        { "_id" : 6, "name" : "abc", "status" : "A", "points" : [ { "points" : 57, "bonus" : 7 } ] }
    

    2、查询NULL值或不存在的键

     //插入文档
        > db.users.insert(
           [
              { "_id" : 900, "name" : null },
              { "_id" : 901 },
              { "_id" : 902,"name" : "Leshami" ,"blog" : "http://blog.csdn.net/leshami"}  
           ]
        )
    
        //查询name自动为null的文档,注,以下查询中,不存在name列的文档_id:901的也被返回
        > db.users.find( { name: null } )
        { "_id" : 900, "name" : null }
        { "_id" : 901 }
    
        //通过$type方式返回name为null的文档,此时_id:901未返回
        > db.users.find( { name : { $type: 10 } } )
        { "_id" : 900, "name" : null }
    
        //通过$exists返回name自动不存在的文档
        > db.users.find( { name : { $exists: false } } )
        { "_id" : 901 }

    五、查询小结

    1、文档查询db.users.find()等价于db.users.find( {} )
    2、基于and运算符的多个组合条件可以省略and运算符的多个组合条件可以省略and,直接将条件组合即可
    3、对于$and运算符内的条件,用[]括起来,相当于数组形式
    4、对于数组查询,可以使用基于下标的方式精确配置特定的元素值
    5、对于内嵌文档,可以使用”文档键.内嵌文档键”方式进行访问
    6、对于数组内内嵌文档的方式,可以使用”数组名.下标.内嵌文档键”方式访问
    7、对于哪些列名需要显示可以通过{ field1: <0|1>, … }来设定
    8、本文参考:https://docs.mongodb.com/manual/tutorial/query-documents/

    文档更新

  • 相关阅读:
    Flume spooldir 报错 MalformedInputException: Input length = 1
    Python股票量化投资-2.量化投资介绍
    Python股票量化投资-1.开发环境部署
    centos 8 安装 activemq5.15 开机自启动
    Hystrix 说明
    Centos 8 安装 tomcat8.5 开机自启动
    Centos 8 安装 JDK 1.8
    centos 8.0 安装 nginx-1.18.0 开机自启动
    Error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)"
    如何复制DataRow(dataTabel中的行)
  • 原文地址:https://www.cnblogs.com/-wenli/p/13347692.html
Copyright © 2020-2023  润新知