Recently developed a version of the functionality of the use of MongoDB paging, lazy to build data, and then studied the next from the production environment to export data to the local test.
Study a bit, found that MongoDB backup restore and MySQL syntax is quite similar, below please see detailed introduction.
One, backup data
Because you need to test the data, you must first back up a copy of the data from the production server (CentOS 6.5),
format : mongodump-h IP--port Port-u user name-p password-d database--drop file exists path
Example:
127.0. 0.1 27017 -u user name-p password-d ad_api_count-o/tmp
(Note that if the password has special characters that require \ Escape under Linux)
You can see the information from the two collections in Ad_api_count by entering the Ad_api_count directory of the backed up data.
Second, restore the database
Download the export database to the local e:\ee\ad_api_count under the production environment, then we can import the data to local MongoDB for testing.
command format : mongorestore-h IP--port Port-u user name-p password-d database file name specific path (suffix format is Bson)
Example:
CD C:\Program files\mongodb\server\3.4127.0. 0.1 27017 -u user name-p password-D ad_api_count E:\cust\ad_api_count\userAdCount.bson
As we know, we've done the restoration work.
Third, other
Mongoexport Export the table, or export some of the fields in the table:
Command format: mongoexport-h IP--port Port-u user name-p password-D database-C table name-F field-Q conditional export--csv-o file name specific path (suffix format can be. dat or. csv);
mongoimport Import the table, or some of the fields in the table :
1. restore a non-CSV file exported from an entire table
Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name specific path (--drop when you do not need to delete the source file can not add)
2. restore the export file for some fields
Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name specific path (--drop when you do not need to delete the source file can not add)
3. restore the exported CSV file (if the data is exported without the--CSV option, the exported data will have a lot of double quotes, the import will fail)
Command format: mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type (CSV)--headerline--upsert--drop file name specific path
Note: If the database has data, to import the most recent data, you need to add the--upsert option, update the data, or you will get an error (Prompt for duplicate key fault collection)
MongoDB backup restores data in one production environment