是JSON方式,也可导出csv格式;
导出为CSV格式代码文件如下:
./mongoexport -d test -c t1 -csv -f num -o t1.dat
-csv:指明了要导出的是CSV格式
-f: 指明需要导出的是哪些例子
数据导入 mongoimport
现将ti删除:
> db.t1.drop()
true
> show collections
system.indexes
system.users
>
再导入数据,这里导入的是csv数据:
./mongoimport -d test -c t1 --type csv --headerline -file /home/t1.dat
connected to: 127.0.0.1
看看导入的数据是不是一样:
> db.t1.find()
{ "_id" : ObjectId("4f8ac746b2764d3c3e2cafbb"), "num" : 1 }
{ "_id" : ObjectId("4f8ac74cb2764d3c3e2cafbc"), "num" : 2 }
{ "_id" : ObjectId("4f8ac74eb2764d3c3e2cafbd"), "num" : 3 }
{ "_id" : ObjectId("4f8ac751b2764d3c3e2cafbe"), "num" : 4 }
{ "_id" : ObjectId("4f8ac755b2764d3c3e2cafbf"), "num" : 5 }
>
--type csv 导入的数据格式为CSV,为什么导入CSV格式:CSV对各大主流的数据库支持更良好,而JSON作为轻量级的数据格式,还有些弊端。
--file 指明导入的文件路径
数据备份和恢复
使用 数据备份 mongodump
./mongodump -d test -o /home/dump
-o:表示输出的备份路径,如果没有使用这个选项的话,MongoDB会自动创建dump文件夹并将备份文件放于其内。
使用数据恢复 mongorestore
mongorestore获取mongodump的输出结果,并将备份的数据插入到运行的MongoDB中。
./mongorestore -d test dump/*
connected to: 127.0.0.1
Thu Apr 19 18:16:12 dump/test/system.users.bson
Thu Apr 19 18:16:12 going into namespace [test.system.users]
2 objects found
Thu Apr 19 18:16:12 dump/test/t1.bson
Thu Apr 19 18:16:12 going into namespace [test.t1]
5 objects found
Thu Apr 19 18:16:12 dump/test/system.indexes.bson
Thu Apr 19 18:16:12 going into namespace [test.system.indexes]
Thu Apr 19 18:16:12 { key: { _id: 1 }, ns: "test.system.users", name: "_id_" }
Thu Apr 19 18:16:13 { key: { _id: 1 }, ns: "test.t1", name: "_id_" }
2 objects found