-------------------MongoDB Data import and Export-------------------
1. Export tool: Mongoexport
1. Concept:
The Mongoexport tool in MongoDB can export a collection to a file in JSON or CSV format. You can specify the exported data item by parameter, or you can export the data based on the specified criteria.
2. Syntax:
mongoexport-d dbname-c collectionname-o file--type json/csv-f field
Parameter description:
-D: Database name
-c:collection Name
-O: The file name of the output
--type: The format of the output, the default is JSON
-F: The Output field, if-type is CSV, you need to add-f "field name"
3. Example:
sudo mongoexport-d mongotest-c users-o/home/python/desktop/mongodb/users.json--type json-f "_id,user_id,user_name,a Ge,status "
2. Data import: Mongoimport
1. Syntax:
mongoimport-d dbname-c collectionname--file filename--headerline--type json/csv-f field
Parameter description:
-D: Database name
-c:collection Name
--type: Imported format default JSON
-F: Imported field names
--headerline: If the imported format is CSV, you can use the header of the first row as the imported field
--file: The file to import
2、示例: sudo mongoimport -d mongotest -c users --file /home/mongodump/articles.json --type json
-------------------MongoDB Backup and Recovery-------------------
1. MongoDB Database backup
1. Syntax:
Mongodump-h dbhost-d Dbname-o dbdirectory
Parameter description:
-H:MONGDB the server address, for example: 127.0.0.1, you can also specify the port number: 127.0.0.1:27017
-D: The database instance that needs to be backed up, for example: test
-O: The data storage location of the backup, for example:/home/mongodump/, of course, the directory needs to be established in advance, this directory contains the database instance backup data.
2. Example:
sudo rm-rf/home/momgodump/
sudo mkdir-p/home/momgodump
sudo mongodump-h 192.168.17.129:27017-d itcast-o/home/mongodump/
2. MongoDB Database Recovery
1. Syntax:
Mongorestore-h dbhost-d dbname--dir dbdirectory
参数或名: -h: MongoDB所在服务器地址 -d: 需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2 --dir: 备份数据所在位置,例如:/home/mongodump/itcast/ --drop: 恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用!2、实例:mongorestore -h 192.168.17.129:27017 -d itcast_restore --dir /home/mongodump/itcast/
MongoDB Import and export and database backup