美文网首页
centos7安装mongo数据库及常用命令 2019-05-2

centos7安装mongo数据库及常用命令 2019-05-2

作者: 我是布谷鸟 | 来源:发表于2019-05-23 20:45 被阅读0次

    目标:

    安装mongo数据库及常用命令。

    准备:

    -centos7
    -网络

    安装mongodb:

    [root@node1 ~]# yum -y install centos-release-openstack-pike
    [root@node1 ~]# yum -y install mongodb mongodb-server      
    

    修改配置文件:

    /etc/mongod.conf下
    删除:bind_ip = 127.0.0.1
    修改:smallfiles = true
    

    启动:

    [root@node1 ~]# systemctl start mongod.service
    

    登录:
    数据库端口:27017

    [root@node1 ~]# mongo 127.0.0.1:27017
    MongoDB shell version: 2.6.11
    connecting to: 127.0.0.1:27017/test
    Server has startup warnings: 
    2019-05-24T17:41:25.977+0000 [initandlisten] 
    2019-05-24T17:41:25.977+0000 [initandlisten] ** WARNING: Readahead for /var/lib/mongodb is set to 4096KB
    2019-05-24T17:41:25.977+0000 [initandlisten] **          We suggest setting it to 256KB (512 sectors) or less
    2019-05-24T17:41:25.977+0000 [initandlisten] **          http://dochub.mongodb.org/core/readahead
    >
    

    安装完成

    常用命令:

    use 库名,如果没有这个数据库则创建:

    > use test;  
    switched to db test
    

    查看数据库列表:

    > show dbs;  
    admin  (empty)
    local  0.031GB
    test   0.031GB
    

    向test_collection集合中插入数据,如果没有这个集合则创建:

    > db.test_collection.insert({"id":"1","name":"XiaoMing"});  
    WriteResult({ "nInserted" : 1 })
    

    查看test数据库下的集合列表:

    > show collections;  
    system.indexes
    test_collection
    

    批量插入,变量“i”作为“id”:

    > for(i=1;i<=100;i++) db.test_collection.insert({"id":i,"name":"test"});  
    WriteResult({ "nInserted" : 1 })
    

    统计查询结果数:

    > db.collection.find().count(); 
    30
    

    批量修改,每条数据的年龄=18+id,test=6+id:

    > for(i=1;i<=30;i++) db.collection.update({"id":i},{"id":i,"name":"test","age":18+i,"test":6+i});  
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    

    查询test_collection集合下所有数据:

    > db.test_collection.find(); 
    { "_id" : ObjectId("5ce82da287c20cb0e031cb1d"), "id" : "1", "name" : "XiaoMing" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb1e"), "id" : 1, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb1f"), "id" : 2, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb20"), "id" : 3, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb21"), "id" : 4, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb22"), "id" : 5, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb23"), "id" : 6, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb24"), "id" : 7, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb25"), "id" : 8, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb26"), "id" : 9, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb27"), "id" : 10, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb28"), "id" : 11, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb29"), "id" : 12, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2a"), "id" : 13, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2b"), "id" : 14, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2c"), "id" : 15, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2d"), "id" : 16, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2e"), "id" : 17, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb2f"), "id" : 18, "name" : "test" }
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb30"), "id" : 19, "name" : "test" }
    Type "it" for more
    

    查询“id”为“1”的行:

    > db.test_collection.find({"id":"1"});  
    { "_id" : ObjectId("5ce82da287c20cb0e031cb1d"), "id" : "1", "name" : "XiaoMing" }
    

    查询“id”为1的行:

    > db.test_collection.find({"id":1});    
    { "_id" : ObjectId("5ce82e4f87c20cb0e031cb1e"), "id" : 1, "name" : "test" }
    

    删除“id”为“1”的行:

    > db.test_collection.remove({"id":"1"});  
    WriteResult({ "nRemoved" : 1 })
    

    删除集合:

    > db.test_collection.drop(); 
    true
    

    删除当前数据库:

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

    删除重复数据:
    根据唯一的“_id”列来删除

    > db.collection.find();
    { "_id" : ObjectId("5ce83107506da8e173b59c56"), "id" : 1, "name" : "test" }
    { "_id" : ObjectId("5ce83119506da8e173b59c57"), "id" : 1, "name" : "test" }
    > db.collection.remove({"_id" : ObjectId("5ce83107506da8e173b59c56")}); 
    WriteResult({ "nRemoved" : 1 })
    

    修改“id“为1的行:

    > db.collection.update({"id":1},{"id":10,"name":"update"}); 
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.collection.find();
    { "_id" : ObjectId("5ce83119506da8e173b59c57"), "id" : 10, "name" : "update" }
    

    查询”id“小于15、”age“大于等于25和”test“在15和20之间的行:

    > db.collection.find({"id":{$lt:15},"age":{$gte:25},"test":{$lte:20,$gte:15}});
    { "_id" : ObjectId("5ce834ae506da8e173b59cd8"), "id" : 9, "name" : "test", "age" : 27, "test" : 15 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cd9"), "id" : 10, "name" : "test", "age" : 28, "test" : 16 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cda"), "id" : 11, "name" : "test", "age" : 29, "test" : 17 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdb"), "id" : 12, "name" : "test", "age" : 30, "test" : 18 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdc"), "id" : 13, "name" : "test", "age" : 31, "test" : 19 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdd"), "id" : 14, "name" : "test", "age" : 32, "test" : 20 }
    

    显示查询结果的前两行:

    > db.collection.find({"id":{$lt:15},"age":{$gte:25},"test":{$lte:20,$gte:15}}).limit(2); 
    { "_id" : ObjectId("5ce834ae506da8e173b59cd8"), "id" : 9, "name" : "test", "age" : 27, "test" : 15 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cd9"), "id" : 10, "name" : "test", "age" : 28, "test" : 16 }
    

    跳过查询结果的前两行:

    > db.collection.find({"id":{$lt:15},"age":{$gte:25},"test":{$lte:20,$gte:15}}).skip(2);  
    { "_id" : ObjectId("5ce834ae506da8e173b59cda"), "id" : 11, "name" : "test", "age" : 29, "test" : 17 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdb"), "id" : 12, "name" : "test", "age" : 30, "test" : 18 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdc"), "id" : 13, "name" : "test", "age" : 31, "test" : 19 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdd"), "id" : 14, "name" : "test", "age" : 32, "test" : 20 }
    

    以”id“的值降序排列:

    > db.collection.find({"id":{$lt:15},"age":{$gte:25},"test":{$lte:20,$gte:15}}).sort({id:-1});  
    { "_id" : ObjectId("5ce834ae506da8e173b59cdd"), "id" : 14, "name" : "test", "age" : 32, "test" : 20 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdc"), "id" : 13, "name" : "test", "age" : 31, "test" : 19 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdb"), "id" : 12, "name" : "test", "age" : 30, "test" : 18 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cda"), "id" : 11, "name" : "test", "age" : 29, "test" : 17 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cd9"), "id" : 10, "name" : "test", "age" : 28, "test" : 16 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cd8"), "id" : 9, "name" : "test", "age" : 27, "test" : 15 }
    

    以”age“的值升序排列:

    > db.collection.find({"id":{$lt:15},"age":{$gte:25},"test":{$lte:20,$gte:15}}).sort({age:1});  
    { "_id" : ObjectId("5ce834ae506da8e173b59cd8"), "id" : 9, "name" : "test", "age" : 27, "test" : 15 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cd9"), "id" : 10, "name" : "test", "age" : 28, "test" : 16 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cda"), "id" : 11, "name" : "test", "age" : 29, "test" : 17 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdb"), "id" : 12, "name" : "test", "age" : 30, "test" : 18 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdc"), "id" : 13, "name" : "test", "age" : 31, "test" : 19 }
    { "_id" : ObjectId("5ce834ae506da8e173b59cdd"), "id" : 14, "name" : "test", "age" : 32, "test" : 20 }
    

    相关文章

      网友评论

          本文标题:centos7安装mongo数据库及常用命令 2019-05-2

          本文链接:https://www.haomeiwen.com/subject/bdhizqtx.html