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
[Root @localhost bin]#
./mongoexport-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 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
[Root @localhost 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
[Root @localhost 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 data import and export and backup recovery