One: Back up the database
G:\Program files\mongodb\server\3.0\bin>mongodump-d Mydb-o G:/data/back
Mongodump-h IP--port Port-u user name-p password-d database-o file exists path
If there are no users, you can remove-u and-P.
If you export the native database, you can remove-H.
If it is the default port, you can remove--port.
If you want to export all databases, you can remove-D.
Export all databases
Command: Mongodump-h 127.0.0.1-o/home/zhangy/mongodb/
Export the specified database
Mongodump-h 192.168.1.108-d Tank-o/home/zhangy/mongodb/
II: Mongorestore RESTORE Database
1, Common command format
Mongorestore-h IP--port Port-u user name-p password-D database-Thedrop file exists a path -drop means, delete all records first, and then restore. /home/zhangy/mongodb/ #这里的路径是所有库的备份
G:\Program files\mongodb\server\3.0\bin>mongorestore-d mydb1--dir g:/data/back/mydb
2. Restore all databases to MongoDB
mongorestore/home/zhangy/mongodb/ #这里的路径是所有库的备份
3. Restore the specified database
G:\Program files\mongodb\server\3.0\bin>mongorestore-d mydb1--dir g:/data/back/mydb-D tank/home/zhangy /mongodb/tank/ -D tank_new/home/zhangy/mongodb/tank/ #将tank还有tank_new数据库中
Third, mongoexport export the table, or some fields in the table
Mongoexport-h IP--port Port-u user name-p password-D database-C table name-F field-Q conditional export--csv-o file name
The above parameters are good to understand, focus on:
-F export refers to field, split by font size,-f name,email,age export name,email,age three fields
-Q can be exported according to the query criteria,-Q ' {"UID": "100"} ' exports UID 100 data
--csv means that the exported file format is CSV, which is useful because most of the relational databases are supported by CSV, and there's something in common here.
Export an entire sheet
mongoexport-d tank-c Users-o/home/zhangy/mongodb/tank/users.dat
Export data based on criteria
mongoexport-d tank-c users-q ' {uid:{$gt: 1}} '-O Tank/users.json
Four, Mongoimport import table, or some fields in table
Restore a non-CSV file exported from an entire table
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name
Focus on--upsert, the other arguments above have been mentioned,--upsert insert or update existing data
Restore an export file for a partial field
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name
--upsertfields Root--upsert like
Restore the exported CSV file
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--type type--headerline--upsert--drop file name
To restore exported table data
mongoimport-d tank-c users--upsert Tank/users.dat
Table data import for partial fields
mongoimport-d tank-c users--upsertfields uid,name,sex tank/users.dat
Restore a CSV file
mongoimport-d tank-c users--type csv--headerline--file tank/users.csv
MongoDB Backup Restore