1、创建库
[ root@localhost ~ ] # mongo
> use school # 创建库school
switched to db school
> show dbs # 不在创建的库里创建集合时,使用show是看不到这个库的
admin 0.000 GB
config 0.000 GB
local 0.000 GB
2、创建集合
> db . createCollection ( 'info' ) # 创建集合,相当于mysql中的表
{ "ok" : 1 }
> show dbs # 就可以使用show查看创建的库了
admin 0.000 GB
config 0.000 GB
local 0.000 GB
school 0.000 GB
> 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.000 GB
config 0.000 GB
local 0.000 GB
myschool 0.000 GB
school 0.000 GB
> use myschool # 删除数据库之前需要进入该数据库
switched to db myschool
> db . dropDatabase ( ) # 删除数据库
{ "dropped" : "myschool" , "ok" : 1 }
> show dbs
admin 0.000 GB
config 0.000 GB
local 0.000 GB
school 0.000 GB
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 - 08 T22: 56 : 20.893 + 0800 connected to: localhost
2021 - 04 - 08 T22: 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 - 08 T22: 59 : 19.474 + 0800 connected to: localhost
2021 - 04 - 08 T22: 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 - 08 T23: 06 : 54.127 + 0800 connected to: localhost
2021 - 04 - 08 T23: 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 - 08 T23: 11 : 29.900 + 0800 writing school . info to
2021 - 04 - 08 T23: 11 : 29.900 + 0800 writing school . test to
2021 - 04 - 08 T23: 11 : 29.913 + 0800 done dumping school . info ( 100 documents )
2021 - 04 - 08 T23: 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指定还原的数据库可以不存在,因为在恢复的时候可以创建该数据库