To use MongoDB, the natural use of data import and export, I learned a bit.
In MONGO study (ii) there are some tools in the Bin directory, this is the use of these tools for data import and export and backup recovery.
Note: The following commands are entered on the cmd command line, and are saved in the current operation directory if the export and backup are not indicated in the directory.
Data export Mongoexport
1. Common Export method: Export data as JSON
Suppose there's an area table in the library with 9 records, and we're going to export it
>c:\mongo\bin\mongoexport-d iflashbuy_log-c Area-o C:\area.dat
Under normal circumstances the output is as follows:
Connected to:127.0.0.1
Exported 9 Records
Parameter description:
-d indicates which library to use, in this case "Iflashbuy_log"
-C indicates the table to be exported, in this case "area"
-O indicates the file name and directory to export, in this case "Area.dat"
From the above you can see how the export is using the JSON style
2. export files in CSV format
>c:\mongo\bin\mongoexport-d iflashbuy_log-c area--csv-f id,province,city-o C:\area.dat
Parameter description:
-csv means to export to CSV format, you must indicate the exported columns after exporting the CSV
-F indicates which columns need to be exported
Data import Mongoimport
1. Import JSON data
A. We will first delete the table area so as to demonstrate the effect db.area.drop ();
B. Execute the import command again:
>c:\mongo\bin\mongoimport-d iflashbuy_log-c Area C:\area.dat
Under normal conditions, output the following:
Connected to:127.0.0.1
Imported 9 objects
You can see that the table structure is created implicitly when importing data.
2. Import CSV Data
A. We will first delete the table area so as to demonstrate the effect db.area.drop ();
B. Execute the import command again
>c:\mongo\bin\mongoimport-d iflashbuy_log-c area--type csv--headerline--file c:\area.data
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:
1. Backing Up the Iflashbuy_log database
>c:\mongo\bin\mongodump-d Iflashbuy_log
A dump directory is created in the current directory to hold the backed-up files, or to specify the directory where the backup resides.
2. Specify directory Backup Iflashbuy_log database
>c:\mongo\bin\mongodump-d Iflashbuy_log-o C:\my_mongodb_dump
In this example, the backup file exists in the C:\my_mongodb_dump directory
Data Recovery Mongorestore
Since the backup has just been done, we will first remove the library Iflashbuy_log
>use Iflashbuy_log
Switched to DB Iflashbuy_log
>db.dropdatabase ()
Next we're going to restore the database
1. Restore the Iflashbuy_log database
>c:\mongo\bin\mongorestore-d My_mongodb C:\my_mongodb_dump
After the validation of the database back, in fact, if you want to restore the library, it is not necessary to delete the Iflashbuy_log library, as long as the--drop parameter, you can delete the table and then insert the data into the table
2. Do not delete the Library recovery library
>c:\mongo\bin\mongorestore-d My_mongodb--drop C:\my_mongodb_dump
At this point, the data import and export of MongoDB and the backup recovery are finished under Windows system.
MongoDB Backup, restore, import and export (CMD command line implementation)