MongoDB Export and Import
1: The import/export can operate on a local MONGODB server, or it can be remote.
Therefore, the following general options are available:
-H Host
--port Port Ports
-u username user name
-P passwd Password
2:mongoexport exporting files in JSON format
Q: Which library, which table, which columns, which rows are exported?
-D Library Name
-C Table Name
-F Field1,field2 ... Column Name
-Q Query criteria
-O Exported file name
--CSV Export CSV format (easy to exchange data with traditional database)
Example 1: Exporting SN and name Stu table SN less than 1000 note: _id columns are always exported
1#./bin/mongoexport-u testuser-p1234-D test-c stu-f sn,name-q'{sn:{$lte: +}}'-O./Test.stu.json2Connected to:127.0.0.13Exported +Records4 [email protected] mongodb]# ll5Total dosage1566Drwxr-xr-x2Root root4096May at Ten: +bin7-RW-------1Hadoop Hadoop34520August4 -gnu-agpl-3.08-RW-------1Hadoop Hadoop1359August4 -README9-rw-r--r--1Root root86786May - Ten: ATest.stu.jsonTen-RW-------1Hadoop Hadoop18436August4 -Third-party-notices
Example 2: Export the format of the STU table CSV
1 1234 -D test-c '{sn:{$lte: ten}}' -O./test.stu.csv
View the exported CSV file
1# MoreTest.stu.csv2 Sn,name3 1.0,"Student1"4 2.0,"Student2"5 3.0,"Student3"6 4.0,"Student4"7 5.0,"student5"8 6.0,"Student6"9 7.0,"student7"Ten 8.0,"student8" One 9.0,"Student9" A 10.0,"student10"
3.Mongoimport Import
-D database to import
-c table to be imported (does not exist to be created by itself)
--type Csv/json (default)
--file backup File path
Example 1: Importing JSON
1 1234 -D test-c Animal--type JSON-file ./test.stu.json
Example 2: Import CSV Note:--headerline Skip column names
1 1234 -D test-c bird-f sn,name--type csv--headline --file ./test.stu.csv
4.Mongodump Exporting binary Bson structure data and its index information
-D Library Name
-C Table Name
-F Field1,field2 ... Column Name
mongodum-d test [-C table name] defaults to the dump directory exported to MONGO
Law:
1: The exported file is placed in a directory named database
2: Export 2 files per table, respectively, Bson structure data file, JSON index information
3: If you do not declare a table name, export all tables
Mongorestore Importing binary files
Cases:
./bin/mongorestore-d Test--directoryperdb dump/test/(backup directory at Mongodump)
Binary backup that not only backs up data, but also backs up indexes,
Backup data is relatively small.
Example: Backup and recovery of single table
1.dump Tea Table
1 1234 -D test-c Tea
2. View the dump file
1 # ll dump/Test2831151 May 10 :panax notoginseng tea.bson41179 May :Panax Notoginseng Tea.metadata.json
3. Delete Tea table
1 > db.tea.drop (); 2 true 3 > show Tables 4animal 5Bird 6 Shop 7 Stu 8 system.indexes 9 system.users Ten >
4. Restore Tea table and view record restore success
1./bin/mongorestore-u Testuser-p1234-D test--collection tea--directoryperdb dump/test/Tea.bson2 3> Db.tea.Find()4{"_id": ObjectId ("5743D98AADDEF29711337FB4"),"Email":"[email protected]" }5{"_id": ObjectId ("5743d98daddef29711337fb5"),"Email":"[email protected]" }6{"_id": ObjectId ("5743d9cfaddef29711337fb7"),"Email":"[email protected]" }7{"_id": ObjectId ("5743DC98ADDEF29711337FBC") }
MongoDB Foundation eight backup and recovery