MongoDB Backup Restore

Source: Internet
Author: User
Tags mongodb mongodump mongorestore

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.