zoukankan      html  css  js  c++  java
  • MongoDB 基础命令 (MongoDB Shell)

    1、我们 mongodb 安装成功后,用上一篇的方法启动 mongodb服务 然后使用 mongodb shell 来做数据库的增删改查

    2、创建数据库

    • 语法:

    use 数据库名称
    • 案例:

    > use juyou
    switched to db juyou
    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB

    这时创建完成过,使用命令查询数据库却没有我们刚创建的数据库,这时因为刚创建的数据库没有数据,下面我们在数据库中插入一条数据

    复制代码
    > db.juyou.insert({"name":"聚优福利"})
    WriteResult({ "nInserted" : 1 })
    > show dbs
    admin   0.000GB
    config  0.000GB
    juyou   0.000GB
    local   0.000GB
    复制代码

    这时就能看到刚刚创建的数据库了

    3、删除数据库

    • 语法:

    db.dropDatabase()
    • 案例:

    首先我们先查询一下所有的数据库

    > show dbs
    admin   0.000GB
    config  0.000GB
    juyou   0.000GB
    local   0.000GB

    然后我们可以使用 db 来查看当前的数据库

    > db
    juyou

    当前链接的数据库就是 juyou,如果不是可以使用 use juyou 命令切换到 juyou 数据库

    > use juyou
    switched to db juyou

    执行删除命令

    > db.dropDatabase()
    { "dropped" : "juyou", "ok" : 1 }

    然后再我们再查询一下所有数据库

    > show dbs
    admin   0.000GB
    config  0.000GB
    local   0.000GB

    已经成功删除了

    4、创建集合

    语法:

    db.createCollection(name, options)
    • name:集合名称
    • options: 可选参数 
    • 案例:

    创建一个名为 userinfo 的集合

    > db.createCollection("userinfo")
    { "ok" : 1 }
    > show collections
    userinfo

    创建成功后可以使用  show collections  命令查询已有集合

     5、插入文档

    • 语法:

    db.集合名称.insert(document)
    • 案例:

    在 juyou 集合下的 userinfo 文档中插入一条数据

    > db.userinfo.insert({name:"郭大爷","sex":"男","age":"不详"})
    WriteResult({ "nInserted" : 1 })
    > db.userinfo.find()
    { "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爷", "sex" : "男", "age" : "不详" }

    插入成功后,可以使用  find()  来查询刚刚插入的数据,下面会对查询做详细的讲解,这里不多做解释

    可以看到插入数据后,多了一列 _id 的数据,在文档中 mongodb 会将 _id 字段自动设置为主键,如果不指定mongodb会自动生成

    自动生成的 ObjectId 是由时间戳、MachineID(电脑的 mac 地址)、进程ID以及自增计数器组成的,基本上不会重复

    > db.userinfo.insert({"_id":1,name:"郭少爷","sex":"男","age":"不详"})
    WriteResult({ "nInserted" : 1 })
    > db.userinfo.find()
    { "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爷", "sex" : "男", "age" : "不详" }
    { "_id" : 1, "name" : "郭少爷", "sex" : "男", "age" : "不详" }

    也可以在插入数据时指定 _id 值,在之前使用mongodb开发中会指定给 _id 值,使用GUID(全球唯一标识)代替

    我们也可以先将要插入的数据定义成变量

    复制代码
    > var user = {name:"郭老师",sex:"男",age:"18"}
    > db.userinfo.insert(user)
    WriteResult({ "nInserted" : 1 })
    > db.userinfo.find()
    { "_id" : ObjectId("5abaf679a3aadbe625070c4f"), "name" : "郭大爷", "sex" : "男", "age" : "不详" }
    { "_id" : 1, "name" : "郭少爷", "sex" : "男", "age" : "不详" }
    { "_id" : ObjectId("5abb05afa3aadbe625070c50"), "name" : "郭老师", "sex" : "男", "age" : "18" }
    复制代码

    mongodb 在3.2版本后 提供了一次插入多条数据的方法 insertMany() ,我们下面把上面的三条数据删除,然后试一下一次插入多条数据

    > db.userinfo.remove({})
    WriteResult({ "nRemoved" : 3 })
    > db.userinfo.find()
    复制代码
    > var users = [
       {
         _id:1,
         name:"郭大爷",
         sex:"男",
         age:"80"
       },
       {
         _id:2,
         name:"郭老师",
         sex:"男",
         age:"不详"
       },
       {
         _id:3,
         name:"郭少爷",
         sex:"男",
         age:"18"
       }
     ]
    > db.userinfo.insertMany(users)
    { "acknowledged" : true, "insertedIds" : [ 1, 2, 3 ] }
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    复制代码

    这样我们可以直接插入一个数组

    6、更新文档

    更新文档有 update() 和 save() 两个方法,接下来分别介绍

    • update() 语法:

    复制代码
    db.collection.update(
       <query>,
       <update>,
       {
         upsert: <boolean>,
         multi: <boolean>,
         writeConcern: <document>
       }
    )

    db.col_name.update({'field1':2},{$set:{'field2':0}})
    复制代码
    • query:条件,相当于sql update时where条件
    • update: 要更新的内容,类似 sql 的 set 后面的内容
    • 案例:

    我们先查询一下,郭老师的年龄是不详,现在我们根据主键_id来把年龄更新成20岁

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.update({"_id":2},{$set:{"age":"20"}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "20" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    复制代码

    已经成功将郭老师的年龄改成20,然后我们看到在更新命令中又一个 $set 的关键词,这个是更新操作符,接下来我们介绍一下操作符

    • $inc:将文档中的数字字段,增加值。比如个郭老师的年龄增加5岁就可以用这个操作符
    • $set:将文档中的字段,更新为传入的字段。上面已经演示过了
    • $unset:将文档中的某个字段删除
    • $rename:给字段重命名
    • (下面的操作符都是用来操作文档中类型是数组的字段)
    • $push:将传入的参数追加到,文档中某个字段中,要追加的字段必须是数组类型
    • $addToSet:在文档某个数组类型的字段中增加值,和上面两个操作符类似,不过这个操作符在增加值时,数组中不能存在要增加的值
    • $pop:删除文档数组类型字段中第一个 {$pop:{name:1}} 或者最后一个值 {$pop:{name:-1}}
    • $pull:删除和传入参数相等的第一个值
    • $pullAll:和 $pull 一样删除值,$pullAll 可以传入数组,一次删除多个值
    • 参考文档:https://blog.csdn.net/u014344668/article/details/52460682
    • save() 语法:

    db.collection.save(
       <document>,
       {
         writeConcern: <document>
       }
    )
    • docment:文档
    • 案例:

    先查询一下所有的用户,然后把_id为2的用户年龄改为不详。save() 方法会根据主键_id为条件替换文档

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "20" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > var user = { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    > db.userinfo.save(user)
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    复制代码
    • 问题:

    save() 方法是以主键_id作条件,来替换文档,如果在传入的文档中没有主键_id,会怎么样?下面我们试一下

    复制代码
    > var user = { "name" : "郭老师", "sex" : "男", "age" : "不详" }
    > db.userinfo.save(user)
    WriteResult({ "nInserted" : 1 })
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    { "_id" : ObjectId("5abb2f42a3aadbe625070c51"), "name" : "郭老师", "sex" : "男", "age" : "不详" }
    复制代码

    也可以执行成功,不过没有更新其中的一个文档,却新插入了一条数据

    7、查询文档

    • 语法:

    db.collection.find(query, projection)
    • query:查询条件
    • 案例:

    可以在 find() 方法后面在 pertty() 方法以格式化的方式显示文档

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    { "_id" : ObjectId("5abb2f42a3aadbe625070c51"), "name" : "郭老师", "sex" : "男", "age" : "不详" }
    > db.userinfo.find().pretty()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    {
        "_id" : ObjectId("5abb2f42a3aadbe625070c51"),
        "name" : "郭老师",
        "sex" : "男",
        "age" : "不详"
    }
    复制代码

    mongodb query 条件与 sql where 条件 对比

    8、$type 操作符

    匹配字段类型

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "age" : 80 }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "age" : 18 }
    { "_id" : ObjectId("5abb2f42a3aadbe625070c51"), "name" : "郭老师", "sex" : "男", "age" : "不详" }
    > db.userinfo.find({"age":{$type:2}})
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : ObjectId("5abb2f42a3aadbe625070c51"), "name" : "郭老师", "sex" : "男", "age" : "不详" }
    复制代码

    上面我们把字段 age 类型为 String 的文档查询出来,下面是 mongodb 中类型和数字的对照表

    9、Limit() 方法与 Skip() 方法

    • Limit()

    • 语法

    db.集合名称.find().limit(数量)

    取集合中指定数量的数据

    • 案例

    复制代码
     db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.find().limit(2)
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    复制代码
    • Skip()

    • 语法

    db.集合名称.find().skip(数量)

    在集合中取数据时跳过指定量数据

    • 案例

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.find().skip(1)
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    复制代码

    这两个方法配合使用,就可以达到分页的目的

    10、排序

    • 语法

    >db.collection.find().sort({key:1})
    • key:指定排序列,1代表正序、-1代表倒序

    • 案例

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.find().sort({"age":1})
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    > db.userinfo.find().sort({"age":-1})
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > 
    复制代码

    上面分别是不排序、根据age正序、根据age倒序查询

    11、删除文档

    • 语法

    db.collection.remove(
       <query>,
       <justOne>
    )
    • query:删除的条件
    • justOne:如果为 true 或者 1,只删除满足条件的第一个文档
    • 案例

    复制代码
    > db.userinfo.find()
    { "_id" : 1, "name" : "郭大爷", "sex" : "男", "age" : "80" }
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.remove({"sex":"男"},true)
    WriteResult({ "nRemoved" : 1 })
    > db.userinfo.find()
    { "_id" : 2, "name" : "郭老师", "sex" : "男", "age" : "不详" }
    { "_id" : 3, "name" : "郭少爷", "sex" : "男", "age" : "18" }
    > db.userinfo.remove({"sex":"男"})
    WriteResult({ "nRemoved" : 2 })
    > db.userinfo.find()
    >
    复制代码

    12、删除集合

    • 语法

    db.collection.drop()
    • 案例

    > show collections
    userinfo
    > db.userinfo.drop()
    true
    > show collections
    > 
  • 相关阅读:
    Codeforces Gym 100571A A. Cursed Query 离线
    codeforces Gym 100500 J. Bye Bye Russia
    codeforces Gym 100500H H. ICPC Quest 水题
    codeforces Gym 100500H A. Potion of Immortality 简单DP
    Codeforces Gym 100500F Problem F. Door Lock 二分
    codeforces Gym 100500C D.Hall of Fame 排序
    spring data jpa 创建方法名进行简单查询
    Spring集成JPA提示Not an managed type
    hibernate配置文件中的catalog属性
    SonarLint插件的安装与使用
  • 原文地址:https://www.cnblogs.com/xiondun/p/12930135.html
Copyright © 2011-2022 走看看