MongoDB安装和简单的删除创建表管理

2023年 7月 15日 73.7k 0

MongoDB系统环境:

[root@MongoDB ~]# lsb_release -a
LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
Distributor ID:    CentOS
Description:    CentOS release 6.7 (Final)
Release:    6.7
Codename:    Final
[root@MongoDB ~]# 

系统初始化:

[root@MongoDB ~]# setenforce 0
[root@MongoDB ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@MongoDB ~]# 

MongoDB 下载位置

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
2.6下载地址:http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/RPMS/

[root@MongoDB ~]# wget http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/RPMS/mongo-10gen-2.4.6-mongodb_1.x86_64.rpm
[root@MongoDB ~]# wget http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/RPMS/mongo-10gen-server-2.4.6-mongodb_1.x86_64.rpm

安装

[root@MongoDB ~]# rpm -ivh *.rpm
Preparing...                ########################################### [100%]
   1:mongo-10gen            ########################################### [ 50%]
   2:mongo-10gen-server     ########################################### [100%]
[root@MongoDB ~]# 

创建数据目录,一般情况下,此目录可能笔记大

[root@MongoDB ~]# mkdir /mongodb/data -pv
mkdir: created directory `/mongodb'
mkdir: created directory `/mongodb/data'
[root@MongoDB ~]# chown -R mongod.mongod /mongodb/data/

修改配置文件中的dbpath路径为创建的目录

[root@MongoDB ~]# cp /etc/mongod.conf{,.bak}
[root@MongoDB ~]# vim /etc/mongod.conf 
dbpath=/mongodb/data

启动

[root@MongoDB ~]# service mongod start
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 3549
all output going to: /var/log/mongo/mongod.log
child process started successfully, parent exiting
                                                           [  OK  ]



[root@MongoDB ~]# ss -lantp |grep :27017
LISTEN     0      128                       *:27017                    *:*      users:(("mongod",3549,10))
[root@MongoDB ~]# ss -lantp |grep :28017
LISTEN     0      128                       *:28017                    *:*      users:(("mongod",3549,9))

28017
27017

mongo默认进入的是test库,使用help可获得帮助db.help 数据库的操作方式

[root@MongoDB ~]# mongo
MongoDB shell version: 2.4.6
connecting to: test
> 

查看到当前数据容量:

> show dbs
admin    (empty)
local    0.078125GB
test    (empty)
> 
test    (empty)

show users 查看用户

> show profile 
db.system.profile is empty 
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries

show logs 查看日志

global
> 

在MongoDB中创建的库必须是有内容的!创建一个库linuxea

> db.linuxea.insert({Name:"Tom"})

查看

> db.linuxea.find()
{ "_id" : ObjectId("564aee8e2fda941bbd334a2c"), "Name" : "Tom" }
> 

> db.linuxea.insert({Name:"jreey"})
> db.linuxea.find()
{ "_id" : ObjectId("564aee8e2fda941bbd334a2c"), "Name" : "Tom" }
{ "_id" : ObjectId("564aef722fda941bbd334a2d"), "Name" : "jreey" }

他的ID编码是Mongodb自己创建的主键,而且默认为索引。

查看stats信息

> db.linuxea.stats()
{
    "ns" : "linuxeadb.linuxea",
    "count" : 2,
    "size" : 76,
    "avgObjSize" : 38,
    "storageSize" : 4096,
    "numExtents" : 1,
    "nindexes" : 1,
    "lastExtentSize" : 4096,
    "paddingFactor" : 1,
    "systemFlags" : 1,
    "userFlags" : 0,
    "totalIndexSize" : 8176,
    "indexSizes" : {
        "_id_" : 8176
    },
    "ok" : 1
}
> 

删除linuxea

> db.linuxea.drop()
true
> show collections
syste

但是show dbs时数据库是还在存在的

[root@MongoDB ~]# cd /mongodb/data/
[root@MongoDB data]# ll
total 294936
drwxr-xr-x. 2 mongod mongod      4096 Nov 17 01:08 journal
-rw-------. 1 mongod mongod  67108864 Nov 17 01:16 linuxeadb.0
-rw-------. 1 mongod mongod 134217728 Nov 17 01:08 linuxeadb.1
-rw-------. 1 mongod mongod  16777216 Nov 17 01:16 linuxeadb.ns
-rw-------. 1 mongod mongod  67108864 Nov 17 00:15 local.0
-rw-------. 1 mongod mongod  16777216 Nov 17 00:15 local.ns
-rwxr-xr-x. 1 mongod mongod         5 Nov 17 00:15 mongod.lock
drwxr-xr-x. 2 mongod mongod      4096 Nov 17 01:08 _tmp
[root@MongoDB data]# 

创建多条语句

> for(i=1;i db.linuxea.find()
{ "_id" : ObjectId("564bdf4eb0b70c92686a4dda"), "Name" : "User1", "Age" : 1, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4ddb"), "Name" : "User2", "Age" : 2, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4ddc"), "Name" : "User3", "Age" : 3, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4ddd"), "Name" : "User4", "Age" : 4, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4dde"), "Name" : "User5", "Age" : 5, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4ddf"), "Name" : "User6", "Age" : 6, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de0"), "Name" : "User7", "Age" : 7, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de1"), "Name" : "User8", "Age" : 8, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de2"), "Name" : "User9", "Age" : 9, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de3"), "Name" : "User10", "Age" : 10, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de4"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de5"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de6"), "Name" : "User13", "Age" : 13, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de7"), "Name" : "User14", "Age" : 14, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de8"), "Name" : "User15", "Age" : 15, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de9"), "Name" : "User16", "Age" : 16, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4dea"), "Name" : "User17", "Age" : 17, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4deb"), "Name" : "User18", "Age" : 18, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4dec"), "Name" : "User19", "Age" : 19, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4ded"), "Name" : "User20", "Age" : 20, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
Type "it" for more
输入it则继续输出

删除
> db.linuxea.remove({Age:10})
> db.linuxea.find()
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de4"), "Name" : "User11", "Age" : 11, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de5"), "Name" : "User12", "Age" : 12, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }

> 

修改:

> db.linuxea.update({Name: "User12"},{$set: {Age:32}})
> db.linuxea.find().limit(15)
{ "_id" : ObjectId("564bdf4eb0b70c92686a4de5"), "Name" : "User12", "Age" : 32, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }

文档个数

> db.linuxea.count()
99

修改字段

> db.linuxea.update({Age: {$gt: 80}},{$set: {Gender: "F"}})
> db.linuxea.find({Age: {$gte: 80}})
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e29"), "Name" : "User80", "Age" : 80, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e2a"), "Name" : "User81", "Age" : 81, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }
修改多个字段
> db.linuxea.update({Age: {$gt: 85}},{$set: {Gender: "F"}},{multi: true})
> db.linuxea.find({Age: {$gte: 80}})
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e2c"), "Name" : "User83", "Age" : 83, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e2d"), "Name" : "User84", "Age" : 84, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e2e"), "Name" : "User85", "Age" : 85, "Gender" : "M", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e30"), "Name" : "User87", "Age" : 87, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e31"), "Name" : "User88", "Age" : 88, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e32"), "Name" : "User89", "Age" : 89, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e33"), "Name" : "User90", "Age" : 90, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }
{ "_id" : ObjectId("564bdf4eb0b70c92686a4e34"), "Name" : "User91", "Age" : 91, "Gender" : "F", "PreferBook" : [  "first book",  "second book" ] }

指定删除字段

db.coLL_NAME.METHOD

常用的方法:

C:insert()
R: find()
U: update()
D: remove()

28017.png

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论