MongoDB export import backup restore data details and examples

  • 2020-06-07 05:29:38
  • OfStack

The importance of database backup and data recovery, I think we all know, here is an example of how to operate data backup, data recovery examples:

Creating test data

Create db:testdb,collection:user, insert 10 records


mongo
MongoDB shell version: 3.0.2
connecting to: test
> use testdb
switched to db testdb
> db.user.insert({id:1,name:" The user 1"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:2,name:" The user 2"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:3,name:" The user 3"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:4,name:" The user 4"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:5,name:" The user 5"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:6,name:" The user 6"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:7,name:" The user 7"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:8,name:" The user 8"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:9,name:" The user 9"});
WriteResult({ "nInserted" : 1 })
> db.user.insert({id:10,name:" The user 10"});
WriteResult({ "nInserted" : 1 })
> 
> db.user.find();
{ "_id" : ObjectId("574d7aae8780832e6c4e27b4"), "id" : 1, "name" : " The user 1" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b5"), "id" : 2, "name" : " The user 2" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b6"), "id" : 3, "name" : " The user 3" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b7"), "id" : 4, "name" : " The user 4" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b8"), "id" : 5, "name" : " The user 5" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27b9"), "id" : 6, "name" : " The user 6" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27ba"), "id" : 7, "name" : " The user 7" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bb"), "id" : 8, "name" : " The user 8" }
{ "_id" : ObjectId("574d7aaf8780832e6c4e27bc"), "id" : 9, "name" : " The user 9" }
{ "_id" : ObjectId("574d7ab08780832e6c4e27bd"), "id" : 10, "name" : " The user 10" }

Export the data to mongoexport

Parameter description:

-ES17en database name
- c collection name
-o output file name
-type output format, default to json
-ES27en output field, if --type is csv, you need to add -ES30en "field name"

For more parameters, please refer to mongoexport? help

Example: Export user all records to /tmp/ user.json


mongoexport -d testdb -c user -o /tmp/user.json
2016-05-31T20:00:32.257+0800  connected to: localhost
2016-05-31T20:00:32.286+0800  exported 10 records

cat /tmp/user.json
{"_id":{"$oid":"574d7aae8780832e6c4e27b4"},"id":1,"name":" The user 1"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b5"},"id":2,"name":" The user 2"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b6"},"id":3,"name":" The user 3"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b7"},"id":4,"name":" The user 4"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b8"},"id":5,"name":" The user 5"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27b9"},"id":6,"name":" The user 6"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27ba"},"id":7,"name":" The user 7"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bb"},"id":8,"name":" The user 8"}
{"_id":{"$oid":"574d7aaf8780832e6c4e27bc"},"id":9,"name":" The user 9"}
{"_id":{"$oid":"574d7ab08780832e6c4e27bd"},"id":10,"name":" The user 10"}

Example: Export user all id to /tmp/ user.csv

Formatting csv but not specifying the field will cause an error


mongoexport -d testdb -c user --type csv -o /tmp/user.csv
2016-05-31T20:01:05.393+0800  Failed: CSV mode requires a field list

mongoexport -d testdb -c user --type csv -f "id" -o /tmp/user.csv
2016-05-31T20:01:46.510+0800  connected to: localhost
2016-05-31T20:01:46.534+0800  exported 10 records

cat /tmp/user.csv
id
1
2
3
4
5
6
7
8
9
10

Data import to mongoimport

Parameter description:

-d database name
- c collection name
-type import format, default json
-ES69en imported field name
If the import is in csv format, you can use the title of line 1 as the imported field
--file files to import

For more parameters, please refer to mongoimport? help

Empty collection user before importing


> db.user.drop();
true
> db.user.find();
> 

Example: Import user. json exported from the previous example


mongoimport -d testdb -c user --file /tmp/user.json
2016-05-31T20:10:22.240+0800  connected to: localhost
2016-05-31T20:10:22.287+0800  imported 10 documents

Example: Import user. csv exported from the previous example


mongoimport -d testdb -c user --type csv --headerline --file /tmp/user.csv
2016-05-31T20:11:28.975+0800  connected to: localhost
2016-05-31T20:11:29.003+0800  imported 10 documents

Data backup mongodump

Parameter description:

-d database name
- c collection name
-ES107en backup file path

For more parameters, please refer to mongodump? help

Example: Back up user of testdb to /tmp


mongodump -d testdb -c user -o /tmp
2016-05-31T20:18:25.813+0800  writing testdb.user to /tmp/testdb/user.bson
2016-05-31T20:18:25.818+0800  writing testdb.user metadata to /tmp/testdb/user.metadata.json
2016-05-31T20:18:25.849+0800  done dumping testdb.user

Data recovery mongorestore

Parameter description:

-d database name
- c collection name

For more parameters, please refer to mongorestore? help

Clear collection user before importing


> db.user.drop();
true
> db.user.find();
>

Example: Restore the data from the above backup


mongorestore -d testdb -c user /tmp/testdb/user.bson 
2016-05-31T20:21:23.050+0800  checking for collection data in /tmp/testdb/user.bson
2016-05-31T20:21:23.084+0800  reading metadata file from /tmp/testdb/user.metadata.json
2016-05-31T20:21:23.088+0800  restoring testdb.user from file /tmp/testdb/user.bson
2016-05-31T20:21:23.153+0800  restoring indexes for collection testdb.user from metadata
2016-05-31T20:21:23.156+0800  finished restoring testdb.user
2016-05-31T20:21:23.156+0800  done

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: