MongoDB import, export, backup, restore

Source: Internet
Author: User
Tags mongodump mongorestore

Data export Mongoexport Suppose there's a user table in the library with 2 records, and we're going 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
[Email protected] bin]#/mongoexport-d my_mongodb-c user-o User.dat
Connected to:127.0.0.1
Exported 2 records
[email protected] bin]# cat User.dat
{"_id": {"$oid": "4F81A4A1779282CA68FD8A5A"}, "UID": 2, "username": "Jerry", "Age": 100}
{"_id": {"$oid": "4f844d1847d25a9ce5f120c4"}, "UID": 1, "username": "Tom", "Age": 25}
[Email protected] bin]#
Parameter description:
-d indicates which library to use, in this case "My_mongodb"
-C indicates the table to be exported, in this case "user"
-O indicates the name of the file to be exported, in this case "User.dat"
From the above you can see how the export is using the JSON style
2 exporting files in CSV format
[Email protected] bin]#/mongoexport-d my_mongodb-c user--csv-f Uid,username,age-o
User_csv.dat
Connected to:127.0.0.1
Exported 2 records
[email protected] bin]# cat User_csv.dat
Uid,username,age
2, "Jerry", 100
1, "Tom", 25
[Email protected] bin]#
Parameter description:
-csv means to export to CSV format
-F indicates which examples need to be exported
More detailed usage can be mongoexport–help to see


Data import Mongoimport


In the previous example we discussed the use of the export tool, and this section will discuss how to import data into a table
1 Importing JSON data
Let's remove the table user first to demonstrate the effect
> Db.user.drop ();
True
> Show Collections;
System.indexes
>
Then import the data
[Email protected] bin]#/mongoimport-d my_mongodb-c user User.dat
Connected to:127.0.0.1
Imported 2 objects
[Email protected] bin]#
You can see that the table structure is created implicitly when importing data

2 Importing CSV data
Let's remove the table user first to demonstrate the effect
> Db.user.drop ();
True
> Show Collections;
System.indexes
>
Then import the data
[Email protected] bin]#./mongoimport-d my_mongodb-c user--type csv--headerline--file
User_csv.dat
Connected to:127.0.0.1
Imported 3 objects
[Email protected] bin]#
Parameter description:
-type indicates the file format to import
-headerline The first row is not imported because the first row is a column name
-file indicates the file path to import
Attention:
CSV format is good, mainstream database support export to CSV format, so this format is very beneficial to heterogeneous data migration


Data backup Mongodump


You can use Mongodump to make a library or table-level backup of MongoDB, as illustrated below:
Backing Up the MY_MONGODB database
[Email protected] 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
[email protected] bin]# LL
Total 67648
-rwxr-xr-x 1 root root 7508756 2011-04-06 bsondump
Drwxr-xr-x 3 root root 4096 04-10 23:54 dump
-rwxr-xr-x 1 root root 2978016 2011-04-06 MONGO
A dump directory is created under the current directory to hold the backed up files
You can also specify the directory where the backup resides.

[Email protected] 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 to
My_mongodb_dump/my_mongodb/system.indexes.bson
1 objects
My_mongodb.user to My_mongodb_dump/my_mongodb/user.bson
2 objects
[Email protected] bin]#
In this example, the backup file exists in the My_mongodb_dump directory under the current directory


Data Recovery Mongorestore


Since the backup has just been done, we will first remove the library My_mongodb
> 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're going to restore the database
[Email protected] bin]#/mongorestore-d my_mongodb my_mongodb_dump/*
Connected to:127.0.0.1
Wed APR 00:03:03 My_mongodb_dump/my_mongodb/user.bson
Wed APR 00:03:03 going into namespace [My_mongodb.user]
Wed APR 00:03:03 2 objects found
Wed APR 00:03:03 My_mongodb_dump/my_mongodb/system.indexes.bson
Wed APR 00:03:03 going into namespace [my_mongodb.system.indexes]
Wed Apr 00:03:03 {name: "_id_", ns: "My_mongodb.user", key: {_id:1}, v:0}
Wed APR 00:03:03 1 objects found
[Email protected] bin]#
After verifying the database back, in fact, if you want to restore the library, it is not necessary to delete the My_mongodb library, as long as it refers to
–drop parameter, you can delete the table and then insert the data into the table when you restore

MongoDB import, export, backup, restore

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.