The mongoexport tool in Mongodb can export a collection to a file in JSON format or CSV format.
Use mongoimport to import a backup exported by mongoexport
-d(database) database to import/export
-c (collection) database table
-u (user) username
-p (password) password
-o specifies the file path to export
--authenticationDatabase + created username (user authenticated user)
export
mongoexport -d wc_mongo_store -c tel_province -u root -p pass -o /mnt/mongo_backup/data.js
The default is to export json format, if you want to export CSV format:
mongoexport -d wc_mongo_store -c tel_province -u root -p pass --csv -f tel,real_province,real_city -o /mnt/mongo_backup/data.csv
import
mongoimport -d wc_mongo_store -c tel_province -u root -p pass --file ./data.js
It is also possible to export the directory using mongodump (for backing up the entire database) and then import using mongorestore
export
mongodump -d wc_mongo_store -u root -p pass -o /mnt/mongo_backup/
import
mongorestore -d new_store --directoryperdb /mnt/mongo_backup/wc_mongo_store/ [--authenticationDatabase admin] -u root -p pass
Here is to import the backup into the new library, probably because the --auth parameter is attached at startup, and the user needs to be created in advance
If the authentication is using a global user, you need to add the following code to indicate the global database to which the account belongs:
--authenticationDatabase admin