MongoDB import and export and database backup
MongoDB data Import and Export
One: export tool: mongoexport
Concept:
The mongoexport tool in mongoDB can export an collection to a file in JSON or CSV format. You can specify exported data items through parameters, or you can export data based on specified criteria.
1. Grammar:
Command format: mongoexport-h IP-- port port-u user name-p password-d database-c table name-f field-Q conditional export-specific path of csv-o file name (suffix format can be .dat or .csv)
Parameter description:
-d: database name
-c: collection name
-o: the file name of the output
-- type: format of the output. Default is json.
-f: output field. If-type is csv, you need to add-f "field name"
2. Example:
Sudo mongoexport-d mongotest-c users-o / data/backup/mongoDB/users.json-- type json-f "_ id,user_id,user_name,age,status" sudo mongoexport-d mongotest-c users-o / data/backup/mongoDB/users.dat-u test-p test123-- slaveOk-- host 10.10.10.110-- port 3777
Second, import tool: mongoimport
1. Grammar:
1: restore non-csv files exported from the whole table
Mongoimport-h IP-- port port-u user name-p password-d database-c table name-upsert-- the specific path of the drop file name (--drop does not need to be deleted when the source file is not added)
2: restore the export files of some fields
Mongoimport-h IP-- port port-u user name-p password-d database-c table name-upsertFields field-specific path of drop file name (--drop can not be added if source file does not need to be deleted)
3: restore the exported csv file (if you export the data without the-- csv option, there will be many double quotes in the exported data, and the import will fail)
Mongoimport-h IP-- port port-u user name-p password-d database-c table name-type type (csv)-headerline-- upsert-- specific path of drop file name
Note: if there is data in the database, if you want to import the latest data, you need to add the-- upsert option, which will update the data, otherwise it will report an error (prompt to repeat key error collection)
Parameter description:
-d: database name
-c: collection name
-- type: imported format defaults to json
-f: imported field name
-- headerline: if the imported format is csv, you can use the title of the first row as the imported field
-- file: the file to import
2. Example:
Mongoimport-d dbname-c collectionname-- file filename-- headerline-- type json/csv-f field
MongoDB backup and recovery
I. MongoDB database backup
1. Grammar:
Mongodump-h dbhost-d dbname-o dbdirectory
Parameter description:
-h: the address of the server where the MongDB is located, for example: 127.0.0.1. Of course, you can also specify the port number: 127.0.0.1 MongDB 27017
-d: database instance that needs to be backed up, for example: test
-o: the location of the backup data, for example: / home/mongodump/, of course, the directory needs to be established in advance, and the backup data of the database instance is stored in this directory.
2. Example:
Sudo rm-rf / home/momgodump/sudo mkdir-p / home/momgodumpsudo mongodump-h 192.168.17.129 home/momgodump/sudo mkdir 27017-d itcast-o / home/mongodump/
II. MongoDB database recovery
1. Grammar:
Mongorestore-h dbhost-d dbname-- dir dbdirectory
Parameter or name:
-h: address of the server where MongoDB is located
-d: the database instance to be restored, such as test. Of course, this name can also be different from that of backup, such as test2.
-- dir: location of backup data, for example: / home/mongodump/itcast/
-- drop: when restoring, delete the current data first, and then restore the backed-up data. That is to say, after recovery, the data added and modified after backup will be deleted, so use it with caution!
2. Example:
Mongorestore-h 192.168.17.129 dir-d itcast_restore-- dir / home/mongodump/itcast/