MongoDB 导出导入备份恢复数据实例

来源:互联网 发布:淘宝怎样取消退款 编辑:程序博客网 时间:2024/05/29 04:48

创建测试数据

创建db:testdb,collection:user,插入10条记录

mongoMongoDB shell version: 3.0.2connecting to: test> use testdbswitched to db testdb> db.user.insert({id:1,name:"用户1"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:2,name:"用户2"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:3,name:"用户3"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:4,name:"用户4"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:5,name:"用户5"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:6,name:"用户6"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:7,name:"用户7"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:8,name:"用户8"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:9,name:"用户9"});WriteResult({ "nInserted" : 1 })> db.user.insert({id:10,name:"用户10"});WriteResult({ "nInserted" : 1 })> > db.user.find();{ "_id" : ObjectId("574d7aae8780832e6c4e27b4"), "id" : 1, "name" : "用户1" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27b5"), "id" : 2, "name" : "用户2" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27b6"), "id" : 3, "name" : "用户3" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27b7"), "id" : 4, "name" : "用户4" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27b8"), "id" : 5, "name" : "用户5" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27b9"), "id" : 6, "name" : "用户6" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27ba"), "id" : 7, "name" : "用户7" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27bb"), "id" : 8, "name" : "用户8" }{ "_id" : ObjectId("574d7aaf8780832e6c4e27bc"), "id" : 9, "name" : "用户9" }{ "_id" : ObjectId("574d7ab08780832e6c4e27bd"), "id" : 10, "name" : "用户10" }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37


数据导出 mongoexport

参数说明:

-d     数据库名-c     collection名-o     输出的文件名--type 输出的格式,默认为json-f     输出的字段,如果--type为csv,则需要加上 -f "字段名"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

更多参数说明可参考 mongoexport –help

例子:导出user所有记录到/tmp/user.json

mongoexport -d testdb -c user -o /tmp/user.json2016-05-31T20:00:32.257+0800    connected to: localhost2016-05-31T20:00:32.286+0800    exported 10 recordscat /tmp/user.json{"_id":{"$oid":"574d7aae8780832e6c4e27b4"},"id":1,"name":"用户1"}{"_id":{"$oid":"574d7aaf8780832e6c4e27b5"},"id":2,"name":"用户2"}{"_id":{"$oid":"574d7aaf8780832e6c4e27b6"},"id":3,"name":"用户3"}{"_id":{"$oid":"574d7aaf8780832e6c4e27b7"},"id":4,"name":"用户4"}{"_id":{"$oid":"574d7aaf8780832e6c4e27b8"},"id":5,"name":"用户5"}{"_id":{"$oid":"574d7aaf8780832e6c4e27b9"},"id":6,"name":"用户6"}{"_id":{"$oid":"574d7aaf8780832e6c4e27ba"},"id":7,"name":"用户7"}{"_id":{"$oid":"574d7aaf8780832e6c4e27bb"},"id":8,"name":"用户8"}{"_id":{"$oid":"574d7aaf8780832e6c4e27bc"},"id":9,"name":"用户9"}{"_id":{"$oid":"574d7ab08780832e6c4e27bd"},"id":10,"name":"用户10"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

例子:导出user所有id到/tmp/user.csv

格式为csv但不指定字段会出错

mongoexport -d testdb -c user --type csv -o /tmp/user.csv2016-05-31T20:01:05.393+0800    Failed: CSV mode requires a field listmongoexport -d testdb -c user --type csv -f "id" -o /tmp/user.csv2016-05-31T20:01:46.510+0800    connected to: localhost2016-05-31T20:01:46.534+0800    exported 10 recordscat /tmp/user.csvid12345678910
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19


数据导入 mongoimport

参数说明:

-d           数据库名-c           collection名--type       导入的格式,默认json-f           导入的字段名--headerline 如果导入的格式是csv,则可以使用第一行的标题作为导入的字段--file       要导入的文件
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

更多参数说明可参考 mongoimport –help

导入前先清空collection user

> db.user.drop();true> db.user.find();> 
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

例子:把上例导出的user.json导入

mongoimport -d testdb -c user --file /tmp/user.json2016-05-31T20:10:22.240+0800    connected to: localhost2016-05-31T20:10:22.287+0800    imported 10 documents
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

例子:把上例导出的user.csv导入

mongoimport -d testdb -c user --type csv --headerline --file /tmp/user.csv2016-05-31T20:11:28.975+0800    connected to: localhost2016-05-31T20:11:29.003+0800    imported 10 documents
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3


数据备份 mongodump

参数说明:

-d 数据库名-c collection名-o 备份的文件路径
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

更多参数说明可参考 mongodump –help

例子:把testdb的user备份到/tmp

mongodump -d testdb -c user -o /tmp2016-05-31T20:18:25.813+0800    writing testdb.user to /tmp/testdb/user.bson2016-05-31T20:18:25.818+0800    writing testdb.user metadata to /tmp/testdb/user.metadata.json2016-05-31T20:18:25.849+0800    done dumping testdb.user
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4


数据恢复 mongorestore

参数说明:

-d 数据库名-c collection名
  • 1
  • 2
  • 1
  • 2

更多参数说明可参考 mongorestore –help

导入前先清空collection user

> db.user.drop();true> db.user.find();>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

例子:把上例备份的数据恢复

mongorestore -d testdb -c user /tmp/testdb/user.bson 2016-05-31T20:21:23.050+0800    checking for collection data in /tmp/testdb/user.bson2016-05-31T20:21:23.084+0800    reading metadata file from /tmp/testdb/user.metadata.json2016-05-31T20:21:23.088+0800    restoring testdb.user from file /tmp/testdb/user.bson2016-05-31T20:21:23.153+0800    restoring indexes for collection testdb.user from metadata2016-05-31T20:21:23.156+0800    finished restoring testdb.user2016-05-31T20:21:23.156+0800    done
0 0
原创粉丝点击