文章目录
1、创建库
[root@localhost ~]# mongo
> use school #创建库school
switched to db school
> show dbs #不在创建的库里创建集合时,使用show是看不到这个库的
admin 0.000GB
config 0.000GB
local 0.000GB
2、创建集合
> db.createCollection('info') #创建集合,相当于mysql中的表
{ "ok" : 1 }
> show dbs #就可以使用show查看创建的库了
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
> show collections #查看集合
info
3、插入、查看数据
> db.info.insert({"id":1,"name":"zhangsan","score":88,"hobby":["game","talk","sport"]}) #插入数据
> db.info.find() #查看集合中的内容
{ "_id" : ObjectId("606f10f0561aaa32ab8f1f80"), "id" : 1, "name" : "zhangsan", "score" : 88, "hobby" : [ "game", "talk", "sport" ] }
4、查找内容
> for (var i=2;i<=100;i++)db.info.insert({"id":i,"name":"jack"+i}) #在集合中使用for循环插入2到100条数据
WriteResult({ "nInserted" : 1 })
> db.info.findOne({"id":10}) #查找id为10的数据内容
{
"_id" : ObjectId("606f11fe561aaa32ab8f1f89"),
"id" : 10,
"name" : "jack10"
}
5、设置别名
> a=db.info.findOne({"id":1}) #定义db.info.findOne({"id":1})的别名为a
{
"_id" : ObjectId("606f10f0561aaa32ab8f1f80"),
"id" : 1,
"name" : "zhangsan",
"score" : 88,
"hobby" : [
"game",
"talk",
"sport"
]
}
> a #此时直接输入a即可查询到id为1的内容
{
"_id" : ObjectId("606f10f0561aaa32ab8f1f80"),
"id" : 1,
"name" : "zhangsan",
"score" : 88,
"hobby" : [
"game",
"talk",
"sport"
]
}
6、查看数据类型
> typeof(a.id) #查看id的字符类型
number #数字类型
> typeof(a.name)
string #字符串类型
> typeof(a.score)
number #数字类型
> typeof(a.hobby)
object #数组类型
7、更改数据内容
> db.info.update({"id":10},{$set:{"name":"tom"}}) #更改id为10的name为tom
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.info.findOne({"id":10}) #查看
{ "_id" : ObjectId("606f11fe561aaa32ab8f1f89"), "id" : 10, "name" : "tom" }
8、删除集合
> db.createCollection('test')
{ "ok" : 1 }
> show tables
info
test
> db.test.drop() #删除集合test
true
> show tables
info
9、删除数据库
> use myschool
switched to db myschool
> db.createCollection('a')
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
myschool 0.000GB
school 0.000GB
> use myschool #删除数据库之前需要进入该数据库
switched to db myschool
> db.dropDatabase() #删除数据库
{ "dropped" : "myschool", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
10、统计集合中数据条数
> use school
switched to db school
> show tables
info
> db.info.count()
100
11、导出数据库中的数据
[root@localhost ~]# mongoexport -d school -c info -o /opt/school.json
2021-04-08T22:56:20.893+0800 connected to: localhost
2021-04-08T22:56:20.895+0800 exported 100 records
###-d:指定数据库 -c:指定集合 -o指定到处绝对路径,注意结尾格式要是json格式
12、导入数据库外的数据到数据库中
[root@localhost opt]# mongoimport -d school -c test --file /opt/school.json
2021-04-08T22:59:19.474+0800 connected to: localhost
2021-04-08T22:59:19.506+0800 imported 100 documents
[root@localhost opt]# mongo #进入数据库中查看导入的数据
> use school
switched to db school
> show tables
info
test
> db.test.find()
{ "_id" : ObjectId("606f10f0561aaa32ab8f1f80"), "id" : 1, "name" : "zhangsan", "score" : 88, "hobby" : [ "game", "talk", "sport" ] }
{ "_id" : ObjectId("606f11fe561aaa32ab8f1f81"), "id" : 2, "name" : "jack2" }
13、限制条件的导出数据
[root@localhost ~]# mongoexport -d school -c info -q '{"id":{"$eq":10}}' -o /opt/top10.json
2021-04-08T23:06:54.127+0800 connected to: localhost
2021-04-08T23:06:54.127+0800 exported 1 record
###-q:后面加限制的条件,这里是限制导出id等于10的数据
###eq:等于 gt:大于 lt:小于
14、备份与恢复数据
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mongodump -d school -o /backup/ #备份数据,-o指定备份保存路径
2021-04-08T23:11:29.900+0800 writing school.info to
2021-04-08T23:11:29.900+0800 writing school.test to
2021-04-08T23:11:29.913+0800 done dumping school.info (100 documents)
2021-04-08T23:11:29.914+0800 done dumping school.test (100 documents)
[root@localhost ~]# ls /backup/school/
info.bson test.bson
info.metadata.json test.metadata.json
[root@localhost ~]# mongorestore -d school2 --dir=/backup/school
####数据库恢复,此处使用-d指定还原的数据库可以不存在,因为在恢复的时候可以创建该数据库
15、复制数据库
> use school
switched to db school
> for (var i=1;i<=100;i++)db.users.insert({"id":1,"name":"jack"+i})
WriteResult({ "nInserted" : 1 })
> show tables #查看usera集合是否创建成功
info
test
users
> db.copyDatabase("school","share") #将school数据库复制为share数据库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
school2 0.000GB
share 0.000GB
> use share
switched to db share
> show tables
info
test
users
16、克隆集合
在不同的实例之间克隆集合
[root@localhost ~]# mkdir -p /data/mongodb/mongodb{2,3,4} #创建多实例存放的数据目录
[root@localhost mongodb]# mkdir /data/mongodb/logs
[root@localhost logs]# touch /data/mongodb/logs/mongodb{2,3,4}.log #创建日志文件
[root@localhost logs]# chmod 777 *.log
[root@localhost logs]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 4月 13 12:12 mongodb2.log
-rwxrwxrwx. 1 root root 0 4月 13 12:12 mongodb3.log
-rwxrwxrwx. 1 root root 0 4月 13 12:12 mongodb4.log
[root@localhost logs]# cd /etc/
[root@localhost etc]# cp -p mongod.conf mongod2.conf
[root@localhost etc]# vim mongod2.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongodb2.log #修改此行
storage:
dbPath: /data/mongodb/mongodb2 #修改此行
journal:
enabled: true
net:
port: 27018 #修改端口号
bindIp: 0.0.0.0
[root@localhost etc]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 83878
child process started successfully, parent exiting
[root@localhost etc]# ss -natp #检查27018端口是否开启
users:(("sshd",pid=4392,fd=9))
LISTEN 0 128 *:27017 *:* users:(("mongod",pid=990,fd=11))
LISTEN 0 128 *:27018 *:* users:(("mongod",pid=83878,fd=11))
[root@localhost etc]# mongo --port 27018 #进入27018端口的数据库
> db.runCommand({"cloneCollection":"school.users","from":"192.168.200.30:27017"})
{ "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
school 0.000GB
> use school
switched to db school
> show tables
users
> db.users.find()
{ "_id" : ObjectId("6075151c649e0a85212e8ad8"), "id" : 1, "name" : "jack1" }
{ "_id" : ObjectId("6075151c649e0a85212e8ad9"), "id" : 1, "name" : "jack2" }
{ "_id" : ObjectId("6075151c649e0a85212e8ada"), "id" : 1, "name" : "jack3" }
17、创建管理用户
> db.createUser({"user":"root","pwd":"123","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> db.auth("root":"123") #进行验证,前提是配置文件要开启验证功能
18、进程管理
> db.currentOp()
显示:
opid:51082
> db.killOp(51082)
{ "info" : "attempting to kill op", "ok" : 1 }