mongodb import and export data

1. Export database data

(For details, see the official website documentation: https://docs.mongodb.com/manual/reference/program/mongoexport/)

([ bin]$ ./mongoexport file directory for performing recovery operations)
sudo ./mongoexport -h server address -u user name -p password -d database name -c collection noun -o export file path and file name.json -- type json --authenticationDatabase admin

Example:
mongoexport -h 121. **. **. ** --port 30011 -u kb *** -p fzmm -d Account -c LinkedIn -o /home/mongodb/LinkedIn.json --type json --authenticationDatabase admin

Example: (output as CSV file)
mongoexport -h 121.**.**.** --port 30011 -u kb*** -p fzmm -d Account -c LinkedIn --sort '{"userState":1}' --type csv -- fields field name to output -o /home/mongodbAccount/linkedin.csv --authenticationDatabase admin
Among them, --sort is optional, indicating that the output is sorted by this field;  --fields must be present, otherwise an error will be reported.


2. Import data
Import data (the test is valid, 2017/9/8, this command is executed under the bin of mongodb)
 mongoimport -h server address--port port number- u username-p password  -d database name-c collection name--upsert --file file path and file name.json
import csv file
mongoimport -h server address --port port number  -u username -p password  -d database name -c collection name --type csv --headerline --upsert --file file path and file name.csv --authenticationDatabase admin

例子:
mongoimport -h 121.**.**.** --port 30011 -u kb*** -p fzmm -d Account -c LinkedIn --upsert --file /home/user.json --authenticationDatabase admin



Garbled characters in CSV Chinese: (quote https://blog.csdn.net/rena521/article/details/50424863)

When exporting the csv file with mongoexport, it is found that the Chinese in the database is displayed as garbled characters in excel, and it is normal to open it with notepad.

Solution: In notepad, change the encoding format to UTF-8, save it, and open it with excel, the Chinese can be displayed normally.

Supplement: (2018/4/19) Today I found that sometimes I use utf-8 garbled characters, but I can display it normally when I replace it with utf-8 BOM.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652983&siteId=291194637