赞
踩
本文介绍使用mongoexport
和mongoimport
命令备份和恢复数据。
mongoexport
命令备份数据库相关命令参数:
$ mongoexport --help
Export MongoDB data to CSV, TSV or JSON files.
options:
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for
-u [ --username ] arg username
-p [ --password ] arg password
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-q [ --query ] arg query filter, as a JSON string
-o [ --out ] arg output file; if not specified, stdout is used
domain-bk.json
文件$ mongoexport -d webmitta -c domain -o domain-bk.json
connected to: 127.0.0.1
exported 10951 records
domain
和worth
字段$ mongoexport -d webmitta -c domain -f "domain,worth" -o domain-bk.json
connected to: 127.0.0.1
exported 10951 records
worth > 100000
的文档$mongoexport -d webmitta -c domain -f "domain,worth" -q '{worth:{$gt:100000}}' -o domain-bk.json
connected to: 127.0.0.1
exported 10903 records
$ mongoexport -h id.mongolab.com:47307 -d heroku_app -c domain -u username123 -p password123 -o domain-bk.json
connected to: id.mongolab.com:47307
exported 10951 records
注意:导出的文档都是json格式
mongoimport
命令恢复数据库相关命令参数:
$ mongoimport --help
connected to: 127.0.0.1
no collection specified!
Import CSV, TSV or JSON data into MongoDB.
options:
-h [ --host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
-u [ --username ] arg username
-p [ --password ] arg password
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-f [ --fields ] arg comma separated list of field names e.g. -f name,age
--file arg file to import from; if not specified stdin is used
--drop drop collection first
--upsert insert or update objects that already exist
domain-bk.json
导入文档,导入的数据库名为webmitta2
,集合名为domain2
。不存在的数据库或集合会被自动创建。$ mongoimport -d webmitta2 -c domain2 --file domain-bk.json
connected to: 127.0.0.1
Wed Apr 10 13:26:12 imported 10903 objects
_id
)$ mongoimport -d webmitta2 -c domain2 --file domain-bk.json --upsert
connected to: 127.0.0.1
Wed Apr 10 13:26:12 imported 10903 objects
domain-bk.json
导入文档$ mongoimport -h id.mongolab.com:47307 -d heroku_app -c domain -u username123 -p password123 --file domain-bk.json
connected to: id.mongolab.com:47307
Wed Apr 10 13:26:12 imported 10903 objects
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。