MongoDB data import/export and Backup Recovery

Source: Internet
Author: User
Tags mongodump mongorestore

Export data using export Export

Suppose there is a user table in the database with two records in it. We want to export it.
> Use my_mongodb
Switched to DB my_mongodb
> DB. User. Find ();
{"_ Id": objectid ("4f81a4a1779282ca68fd8a5a"), "uid": 2, "username": "Jerry", "Age": 100}
{"_ Id": objectid ("4f844d1847d25a9ce5f120c4"), "uid": 1, "username": "Tom", "Age": 25}
>
1. Common export Methods
[Root @ localhost bin] #./export-D my_mongodb-C user-o User. dat
Connected to: 127.0.0.1
Exported 2 records
[Root @ localhost bin] # Cat user. dat
{"_ Id": {"$ oid": "4f81a4a1779282ca68fd8a5a" }," uid ": 2," username ":" Jerry "," Age ": 100}
{"_ Id": {"$ oid": "4f844d1847d25a9ce5f120c4"}, "uid": 1, "username": "Tom", "Age": 25}
[Root @ localhost bin] #
Parameter description:
-D indicates the database to be used. In this example, it is "my_mongodb"
-C: Specify the table to be exported. In this example, it is "user"
-O indicates the file name to be exported. In this example, it is "user. dat"
We can see from the above that the export method uses the JSON style.
2. Export CSV files
[Root @ localhost bin] #./export-D my_mongodb-C user -- CSV-F uid, username, age-o
User_csv.dat
Connected to: 127.0.0.1
Exported 2 records
[Root @ localhost bin] # Cat user_csv.dat
UID, username, age
2, "Jerry", 100
1, "Tom", 25
[Root @ localhost bin] #
Parameter description:
-CSV indicates the CSV format to be exported.
-F indicates which instances need to be exported
For more detailed usage, refer to export-help.

Data Import into import

In the preceding example, we will discuss how to import data to a table by using the export tool.
1. Import JSON data
Delete the table user to demonstrate the effect.
> DB. User. Drop ();
True
> Show collections;
System. Indexes
>
Then import the data
[Root @ localhost bin] #./configure import-D my_mongodb-C user. dat
Connected to: 127.0.0.1
Imported 2 objects
[Root @ localhost bin] #
We can see that the table structure is implicitly created during data import.

2. Import CSV data
Delete the table user to demonstrate the effect.
> DB. User. Drop ();
True
> Show collections;
System. Indexes
>
Then import the data
[Root @ localhost bin] #./Export Import-D my_mongodb-C user -- type CSV -- headerline -- File
User_csv.dat
Connected to: 127.0.0.1
Imported 3 objects
[Root @ localhost bin] #
Parameter description:
-Type indicates the format of the file to be imported.
-Headerline: do not import the first line because the first line is the column name.
-File: Specifies the path of the file to be imported.
Note:
The CSV format is good. Mainstream databases support exporting to the CSV format. Therefore, this format is very helpful for heterogeneous data migration.

Mongodump


Mongodump can be used for database or table-level backup of MongoDB. The following is an example:
Back up the my_mongodb Database
[Root @ localhost bin] #./mongodump-D my_mongodb
Connected to: 127.0.0.1
Database: my_mongodb to dump/my_mongodb
My_mongodb.system.indexes to dump/my_mongodb/system. Indexes. bson
1 objects
My_mongodb.user to dump/my_mongodb/user. bson
2 objects
[Root @ localhost bin] # ll
Total 67648
-Rwxr-XR-x 1 Root 7508756 bsondump
Drwxr-XR-x 3 Root 4096 04-10 23:54 dump
-Rwxr-XR-x 1 Root 2978016 Mongo
A dump directory is created under the current directory to store the backed up files.
You can also specify the directory where the backup is stored,

[Root @ localhost bin] #./mongodump-D my_mongodb-O my_mongodb_dump
Connected to: 127.0.0.1
Database: my_mongodb to my_mongodb_dump/my_mongodb
My_mongodb.system.indexes
My_mongodb_dump/my_mongodb/system. Indexes. bson
1 objects
My_mongodb.user to my_mongodb_dump/my_mongodb/user. bson
2 objects
[Root @ localhost bin] #
In this example, the backup file is stored in the my_mongodb_dump directory under the current directory.

Data Recovery mongorestore


We have just backed up the database, so we will delete the database my_mongodb first.
> Use my_mongodb
Switched to DB my_mongodb
> DB. dropdatabase ()
{"Dropped": "my_mongodb", "OK": 1}
> Show DBS
Admin (empty)
Local (empty)
Test (empty)
>
Next, we will restore the database.
[Root @ localhost bin] #./mongorestore-D my_mongodb my_mongodb_dump /*
Connected to: 127.0.0.1
Wed Apr 11 00:03:03 my_mongodb_dump/my_mongodb/user. bson
Wed Apr 11 00:03:03 going into namespace [my_mongodb.user]
Wed Apr 11 00:03:03 2 objects found
Wed Apr 11 00:03:03 my_mongodb_dump/my_mongo/ system. Indexes. bson
Wed Apr 11 00:03:03 going into namespace [my_mongodb.system.indexes]
Wed Apr 11 00:03:03 {name: "_ id _", NS: "my_mongodb.user", key: {_ ID: 1}, V: 0}
Wed Apr 11 00:03:03 1 objects found
[Root @ localhost bin] #
After verification, the database is returned. In fact, if you want to restore the database, you do not have to delete the my_mongodb database first.
If the-drop parameter is specified, you can delete the table before inserting data into the table.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.