一、全库备份恢复
创新新互联,凭借10多年的网站设计、成都网站建设经验,本着真心·诚心服务的企业理念服务于成都中小企业设计网站有上1000+案例。做网站建设,选创新互联建站。#mongodump -d my_mongodb -o my_mongodb_dump
#mongorestore -d my_mongodb my_mongodb_dump/*
二、JSON Collection备份恢复
$ mongoexport -d test -c book -o book.json
connected to: 10.77.20.xx
exported 2 records
$ mongoimport -d test -c book book.json
connected to: 10.77.20.xx
imported 2 objects
三、CSV Collection数据导出导入
# more students.txt
{ "classid" : 1, "age" : 20, "name" : "kobe" }
{ "classid" : 1, "age" : 23, "name" : "nash" }
{ "classid" : 2, "age" : 18, "name" : "james" }
{ "classid" : 2, "age" : 19, "name" : "wade" }
{ "classid" : 2, "age" : 19, "name" : "bosh" }
{ "classid" : 2, "age" : 25, "name" : "allen" }
{ "classid" : 1, "age" : 19, "name" : "howard" }
{ "classid" : 1, "age" : 22, "name" : "paul" }
{ "classid" : 2, "age" : 24, "name" : "shane" }
#
1.导入数据
# mongoimport -d test -c students students.txt
connected to: 127.0.0.1
Tue Apr 22 16:02:32 imported 9 objects
> db.students.find()db.students.find()
{ "_id" : ObjectId("53562218bc98e02b24984175"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("53562218bc98e02b24984176"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("53562218bc98e02b24984177"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("53562218bc98e02b24984178"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("53562218bc98e02b24984179"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("53562218bc98e02b2498417a"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("53562218bc98e02b2498417b"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("53562218bc98e02b2498417c"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("53562218bc98e02b2498417d"), "classid" : 2, "age" : 24, "name" : "shane" }
>
2.导出CSV
# mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
connected to: 127.0.0.1
exported 9 records
#
导出只有数据,不包括_id
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
[root@jsmartserv hbk]# more students_csv.dat
classid,name,age
1,"kobe",20
1,"nash",23
2,"james",18
2,"wade",19
2,"bosh",19
2,"allen",25
1,"howard",19
1,"paul",22
2,"shane",24
[root@jsmartserv hbk]#
3.删除数据
db.students.remove()
4.导入CSV
# mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
Tue Apr 22 16:07:57 imported 9 objects
参数说明:
-type:指明要导入的文件格式
-headerline:指明第一行是列名,不需要导入
-file:指明要导入的文件
5.验证
db.students.find()db.students.find()
{ "_id" : ObjectId("5356235dbc98e02b2498417e"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("5356235dbc98e02b2498417f"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("5356235dbc98e02b24984180"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("5356235dbc98e02b24984181"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984182"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984183"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("5356235dbc98e02b24984184"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984185"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("5356235dbc98e02b24984186"), "classid" : 2, "name" : "shane", "age" : 24 }
>
查询students集合,验证导入是否成功:
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。