MongoDB Database Operations--Backup restore Export Import

Source: Internet
Author: User
Tags mongodump mongorestore

Tag: base font size Field No query exp NTS index CIN

Original: http://www.jb51.net/article/52498.htm

One, Mongodump backup database

1, Common command grid

?
1 mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径

If there is no user who 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.

2. Export all databases

?
12345678910 [[email protected] mongodb]# mongodump -h 127.0.0.1 -o /home/zhangy/mongodb/ connected to: 127.0.0.1 Tue Dec 3 06:15:55.448 all dbs Tue Dec 3 06:15:55.449 DATABASE: testto   /home/zhangy/mongodb/testTue Dec 3 06:15:55.449   test.system.indexes to /home/zhangy/mongodb/test/system.indexes.bson Tue Dec 3 06:15:55.450     1 objects Tue Dec 3 06:15:55.450   test.posts to /home/zhangy/mongodb/test/posts.bson Tue Dec 3 06:15:55.480     0 objects  。。。。。。。。。。。。。。。。。。。。省略。。。。。。。。。。。。。。。。。。。。。。。。。。

3, export the specified database

?
1234567891011 [[email protected] mongodb]# mongodump -h 192.168.1.108 -d tank -o /home/zhangy/mongodb/ connected to: 192.168.1.108 Tue Dec 3 06:11:41.618 DATABASE: tank   to   /home/zhangy/mongodb/tankTue Dec 3 06:11:41.623   tank.system.indexes to /home/zhangy/mongodb/tank/system.indexes.bson Tue Dec 3 06:11:41.623     2 objects Tue Dec 3 06:11:41.623   tank.contact to /home/zhangy/mongodb/tank/contact.bson Tue Dec 3 06:11:41.669     2 objects Tue Dec 3 06:11:41.670   Metadata for tank.contact to /home/zhangy/mongodb/tank/contact.metadata.json Tue Dec 3 06:11:41.670   tank.users to /home/zhangy/mongodb/tank/users.bson Tue Dec 3 06:11:41.685     2 objects Tue Dec 3 06:11:41.685   Metadata for tank.users to /home/zhangy/mongodb/tank/users.metadata.json

Third, Mongorestore restore the database

1, Common command format

?
1 mongorestore -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 --drop 文件存在路径


--drop means to delete all records first and then restore them.

2. Restore all databases to MongoDB

?
1 [[email protected] mongodb]# mongorestore /home/zhangy/mongodb/  #这里的路径是所有库的备份路径


3. Restore the specified database

?
123 [[email protected] mongodb]# mongorestore -d tank /home/zhangy/mongodb/tank/  #tank这个数据库的备份路径  [[email protected] mongodb]# mongorestore -d tank_new /home/zhangy/mongodb/tank/  #将tank还有tank_new数据库中


These two commands can be implemented to backup and restore the database, the file format is JSON and Bson. Unable to refer to a table backup or restore.

Four, Mongoexport export the table, or some fields in the table

1, Common command format

?
1 mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名

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 export the root query condition,-Q ' {"UID": "100"} ' exports the data with UID 100
--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.

2, export the whole sheet

?
123 [[email protected] mongodb]# mongoexport -d tank -c users -o /home/zhangy/mongodb/tank/users.dat connected to: 127.0.0.1 exported 4 records

3. Export some fields from the table

?
123 [[email protected] mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv connected to: 127.0.0.1 exported 4 records

4, according to the conditions to dare to out data

?
123 [[email protected] mongodb]# mongoexport -d tank -c users -q ‘{uid:{$gt:1}}‘ -o tank/users.json connected to: 127.0.0.1 exported 3 records

Five, Mongoimport import table, or partial field in table

1, Common command format

1.1. Restore a non-CSV file exported from the whole 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
1.2. Restore the export file for some fields
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name
--upsertfields Root--upsert like
1.3. 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
In the above three cases, there can be other permutations and combinations.

2. Restore the exported table data

?
123 [[email protected] mongodb]# mongoimport -d tank -c users --upsert tank/users.dat connected to: 127.0.0.1 Tue Dec 3 08:26:52.852 imported 4 objects

3. Table data import for some fields

[Email protected] mongodb]# mongoimport-d tank-c users--upsertfields uid,name,sex tank/users.dat
Connected to:127.0.0.1
Tue DEC 3 08:31:15.179 imported 4 objects

4. Restore the CSV file

?
123 [[email protected] mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank/users.csv connected to: 127.0.0.1 Tue Dec 3 08:37:21.961 imported 4 objects

Overall feeling, MongoDB backup and restore, or quite powerful, though a little troublesome.

MongoDB Database Operations--Backup restore Export Import

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.