• mongo笔记


    设置数据目录

    mongod --dbpath 目录

    设置端口

    mongod --port 8888

    如果不设置,则使用默认端口号 27017

    进入mongo shell界面 mongodb的交互界面用来操作数据库

    mongo

    退出 mongo shell

    quit()

    查看数据库

    show dbs;
    show databases;
    

    查看当前数据库

    db
    

    切换数据库

    use db_name  
    

    创建数据库

    use db_name   #和切换数据库命令一样
    不需要明确创建数据库,直接使用use db_name 使用即可
    

    删除数据库

    db.dropDatabase()
    

    默认数据库信息

    admin 存储用户

    local 存储本地数据

    config 存储分片配置信息

    mongo系统全局变量

    db 代表当前正在使用的数据库

    db 默认为test,如果插入数据即创建test数据库

    数据库的备份和恢复

    将本机下,test数据库备份到stu目录中

    mongodump -h 1237.0.0.0 -d test-o stu

    -h 主机名

    -d 数据库

    -o 指定保存目录

    [root@localhost ~]# mongodump -h 127.0.0.1 -d test -o stu
    2022-05-24T22:19:52.853-0400    writing test.stu to stu/test/stu.bson
    2022-05-24T22:19:52.854-0400    done dumping test.stu (1 document)
    [root@localhost stu]# ls
    test
    [root@localhost stu]# cd test/
    [root@localhost test]# ls
    stu.bson  stu.metadata.json
    
    

    恢复数据库可以恢复到其他数据库中,从a库备份,在恢复到b库

    恢复

    mongorestore -h host:port -d db_name <path>

    讲test目录下得文件恢复到test1数据库中

    [root@localhost test]# mongorestore  -h 127.0.0.1:27017 -d test1 ../test
    2022-05-24T22:26:38.462-0400    The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
    2022-05-24T22:26:38.462-0400    building a list of collections to restore from ../test dir
    2022-05-24T22:26:38.462-0400    reading metadata for test1.stu from ../test/stu.metadata.json
    2022-05-24T22:26:38.466-0400    restoring test1.stu from ../test/stu.bson
    2022-05-24T22:26:38.477-0400    finished restoring test1.stu (1 document, 0 failures)
    2022-05-24T22:26:38.477-0400    no indexes to restore for collection test1.stu
    2022-05-24T22:26:38.477-0400    1 document(s) restored successfully. 0 document(s) failed to restore.
    
    

    数据库监测命令

    mongostat

    [root@localhost test]# mongostat
    insert query update delete getmore command dirty used flushes vsize  res qrw arw net_in net_out conn                time
        *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   111b   52.6k   19 May 24 22:31:45.684
        *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   112b   52.8k   19 May 24 22:31:46.682
        *0    *0     *0     *0       0     3|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   428b   53.6k   19 May 24 22:31:47.682
        *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   111b   52.7k   19 May 24 22:31:48.683
        *0    *0     *0     *0       0     2|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   167b   53.0k   19 May 24 22:31:49.682
        *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   112b   52.7k   19 May 24 22:31:50.682
        *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   111b   52.6k   19 May 24 22:31:51.683
        *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   112b   52.8k   19 May 24 22:31:52.682
        *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   111b   52.7k   19 May 24 22:31:53.683
        *0    *0     *0     *0       0     2|0  0.0% 0.0%       0 1.55G 113M 0|0 0|0   167b   53.1k   19 May 24 22:31:54.682
        
        
        insert query update delete 每秒增查改删的次数
        getmore 每秒运行命令次数
        dirty used flushes 每秒刷新磁盘次数,每秒操作磁盘的次数
         vsize  res  使用虚拟内存和物理内存情况
    

    mongotop

    检测每个数据库的读写时长

    [root@localhost test]# mongotop
    2022-05-24T22:37:39.065-0400    connected to: mongodb://localhost/
    
                        ns    total    read    write    2022-05-24T22:37:40-04:00
      admin.system.version      0ms     0ms      0ms
        config.collections      0ms     0ms      0ms
    config.system.sessions      0ms     0ms      0ms
       config.transactions      0ms     0ms      0ms
         local.startup_log      0ms     0ms      0ms
      local.system.replset      0ms     0ms      0ms
                  test.stu      0ms     0ms      0ms
                 test1.stu      0ms     0ms      0ms
                 test2.stu      0ms     0ms      0ms
    
                        ns    total    read    write    2022-05-24T22:37:41-04:00
      admin.system.version      0ms     0ms      0ms
        config.collections      0ms     0ms      0ms
    config.system.sessions      0ms     0ms      0ms
       config.transactions      0ms     0ms      0ms
         local.startup_log      0ms     0ms      0ms
      local.system.replset      0ms     0ms      0ms
                  test.stu      0ms     0ms      0ms
           数据集合				总时长		读时长    写时长
    

    删除数据库

    db.dropDatabase()

    删除db所代表的数据库

    集合的创建

    db.createCollection(collection_name)

    > db.createCollection("class2")
    { "ok" : 1 }
    >
    
    

    查看集合

    show tables 或者 show collections

    > show tables;
    class2
    stu
    > show collections;
    class2
    stu
    
    

    创建集合2

    当向一个集合中插入一个文档时,如果该文档不存在则自动创建

    db.collectionName.insert()

    > db.class0.insert({a:1})
    WriteResult({ "nInserted" : 1 })
    > show tables;
    class0
    class2
    stu
    
    
    

    删除集合

    db.collectionName.drop()

    > show collections;
    class0
    class2
    stu
    > db
    test
    > db.class0.drop()
    true
    >
      
    

    集合重命名

    db.collectionName.renameCollection('new_name')

    >  show collections
    class2
    stu
    > db.stu.renameCollection('stu2');
    { "ok" : 1 }
    > show collections;
    class2
    stu2
    
    
    

    文档

    插入文档

    db.stu.ininsert({})

    > show tables;
    class0
    stu2
    > db.class0.insert({'name':'lyw','age':15,'sex':'w'});
    WriteResult({ "nInserted" : 1 })
    > db.class0.find();
    { "_id" : ObjectId("628da60e831a377935c08780"), "name" : "lyw", "age" : 15, "sex" : "w" }
    

    插入多条文档

    db.sru.insert([{},{},{}])

    > db.stu.insert([{name:'zs'},{name:'ls'}]);
    BulkWriteResult({
            "writeErrors" : [ ],
            "writeConcernErrors" : [ ],
            "nInserted" : 2,
            "nUpserted" : 0,
            "nMatched" : 0,
            "nModified" : 0,
            "nRemoved" : 0,
            "upserted" : [ ]
    })
    > db
    test
    > db.stu.find();
    { "_id" : ObjectId("628db6cab93db435c40e80d2"), "name" : "zs" }
    { "_id" : ObjectId("628db6cab93db435c40e80d3"), "name" : "ls" }
    
    

    _id为系统自动添加主键,如果自己写_id 域则会使用自己写的值,但是该值仍不允许重复。

    save插入数据

    db.collectionName.save()

    新增数据

    > db.stu.save({name:'tony'})
    WriteResult({ "nInserted" : 1 })
    > db.stu.find();
    { "_id" : ObjectId("628db6cab93db435c40e80d2"), "name" : "zs" }
    { "_id" : ObjectId("628db6cab93db435c40e80d3"), "name" : "ls" }
    { "_id" : ObjectId("628dba4bb93db435c40e80d4"), "name" : "tony" }
    

    插入数据,如果_id存在,则修改数据;如果不存在则是新增操作

    > db.stu.save({name:'tony',age:18})
    WriteResult({ "nInserted" : 1 })
    > db.stu.find();
    { "_id" : ObjectId("628db6cab93db435c40e80d2"), "name" : "zs" }
    { "_id" : ObjectId("628db6cab93db435c40e80d3"), "name" : "ls" }
    { "_id" : ObjectId("628dba4bb93db435c40e80d4"), "name" : "tony" }
    { "_id" : ObjectId("628dbad4b93db435c40e80d5"), "name" : "tony", "age" : 18 }
    > db.stu.save({ "_id" : ObjectId("628dbad4b93db435c40e80d5"), "name" : "tony", "age" : 19 })
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.stu.find();
    { "_id" : ObjectId("628db6cab93db435c40e80d2"), "name" : "zs" }
    { "_id" : ObjectId("628db6cab93db435c40e80d3"), "name" : "ls" }
    { "_id" : ObjectId("628dba4bb93db435c40e80d4"), "name" : "tony" }
    { "_id" : ObjectId("628dbad4b93db435c40e80d5"), "name" : "tony", "age" : 19 }
    
    

    在不加_id时使用同insert

    如果使用save的时候加了_id_id的值不存在则正常插入数据,如果存在则修改原来的内容

    save无法一次插入多个文档

    集合创建删除

    db.createCollection();
    db.stu.insert({})
    

    插入练习

    db.stu.insert({})
    db.stu.insert([{},{},{}])
    db.stu.save({})
    

    关系型数据库和非关系型数据库都有什么特点

    mongodb的有点在哪里

    db.getCollection() =====db.collectionName

    db.getCollection('stu')====db.stu

    > db.getCollection('stu').insert({email:'123@qq.com'});
    WriteResult({ "nInserted" : 1 })
    > db.stu.find();
    { "_id" : ObjectId("628db6cab93db435c40e80d2"), "name" : "zs" }
    { "_id" : ObjectId("628db6cab93db435c40e80d3"), "name" : "ls" }
    { "_id" : ObjectId("628dba4bb93db435c40e80d4"), "name" : "tony" }
    { "_id" : ObjectId("628dbad4b93db435c40e80d5"), "name" : "tony", "age" : 19 }
    { "_id" : ObjectId("628dc013bf2c2a93c4569af0"), "email" : "123@qq.com" }
    

    查找操作

    find(query,field)

    功能:查找所有符合条件的文档

    参数:query 筛选条件 相当于where子句

    ​ field 展示的域 相当于select的展示部分

    返回 : 所有查找到的内容

    field参数:选择要展示的域,传一组键值对

    键表示域名:值表示是否显示该域 0表示不显示,1表示显示

    > db.class0.find({},{name:1,age:1})
    { "_id" : ObjectId("628da60e831a377935c08780"), "name" : "lyw", "age" : 15 }
    > db.class0.find({},{name:1})
    { "_id" : ObjectId("628da60e831a377935c08780"), "name" : "lyw" }
    
    

    如果某个域给定0,则表示不显示该域,其他的域均显示;

    如果某个域给定1,则表示显示该域,其他的域都不显示;

    _id永远默认为显示,除非设置为0

    _id外,其他域必须要设置为相同的值,全为0或者全为1,不能交叉设置,

    如果不写改参数,则表示显示所有域的内容类似于sql中的* (查询所有字段)

    db.class0.find({},{_id,name:1,age:1})
    只显示name和age 
    

    query:以键值对的形式给出查找条件

    查询age=1700的数据,不显示_id

    db.class0.find({age:1700},{_id:0})
    
    查询年龄在15岁的数据,不显示id
    
    > db.class0.find({age:15},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    

    findOne()

    功能:功能和参数和find完全相同,只是只返回第一条查找到的文档

    query的更多用法:

    比较操作符

    $eq 等于

    $lt 表示小于

    $gt 大于

    $lte 小于等于

    $gte 大于等于

    $ne 不等于

    $in 包含

    $nin 不包含

     
    > db.class0.find({age:{$eq:15}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$lt:20}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$gt:10}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$gte:10}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$lte:20}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$ne:20}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$in:[15,16,17,18]}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    > db.class0.find({age:{$nin:[16,17,18]}},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    
    
    

    逻辑操作符

    $and 逻辑与

    > db.class0.find({age:15,sex:'w'},{_id:0})
    { "name" : "lyw", "age" : 15, "sex" : "w" }
    
    
    年龄小于19 并且性别为男
    db.class0.find({age:{$lt:19},sex:'m'},{_id:0})
    
    年龄小于19 并且大于15
    db.class0.find({age:{$lt:19,$gt:15}},{_id:0})
    
    
    db.class0.find({$and:[{age:17},{name:'Lei'}]},{_id:0})
    

    $or 逻辑或

    年龄小于22或者名字叫悟空的文档
    db.class0.find({$or:[{age:{$lt:22}},{name:'悟空'}]},{_id:0})
    
    年龄小于18或者年龄大于25的
    db.class0.find({$or:[{age:{$lt:18}},{age:{$gt:25}}]},{_id:0})
    

    $not 逻辑非

    
    db.class0.find({age:{$not:{$eq:17}}},{_id:0})
    
    

    $nor 既不也不 表示集合中的条件都不具备

    年龄既不大于18 性别也不是男的
    
    db.class0.find({$nor:[{age:{$gt:18},{sex:'m'}},{}],{_id:0})
    
    
    (年龄小于20  或者 姓名为Lily) 并且 性别为女的人
    
    db.class0.find({$and:[{$or:[{age:{$lt:20}},{name:"Lily"}]},{sex:'female'}]},{_id:0})
    
    年龄小于等于17或者  (姓名大于tom 并且年龄大于100)
    db.classs0.find({$or:[{age:{$lte:17}},{$and:[{name:{$gt:'tom'}},{age:{$gt:100}}]}]},{_id:0})
    
  • 相关阅读:
    IIS“服务没有及时响应启动或控制请求”错误解决
    CSS Overflow属性详解
    访问二维数组的实例ActionScript
    mailto语法
    IIS重新注册asp.net
    flash 动态文本 html
    C++继承中构造函数、析构函数调用顺序及虚析构函数
    根据指定两个日期计算出这些时间内有多少天是周末 php程序函数代码
    计算一段日期内的周末天数(星期六,星期日总和)(
    计算一段日期内的周末天数(星期六,星期日总和
  • 原文地址:https://www.cnblogs.com/liangyongwang/p/16309647.html
Copyright © 2020-2023  润新知