MongoDB 导入导出

来源:互联网 发布:考辛斯体测数据 编辑:程序博客网 时间:2024/06/05 11:39

本文介绍使用mongoexportmongoimport命令备份和恢复数据。

1. 使用mongoexport命令备份数据库

相关命令参数:

$ mongoexport --helpExport 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

1.1 导出所有文档所有字段到文件domain-bk.json文件

$ mongoexport -d webmitta -c domain -o domain-bk.jsonconnected to: 127.0.0.1exported 10951 records

1.2 导出所有哦文档的domainworth字段

$ mongoexport -d webmitta -c domain -f "domain,worth" -o domain-bk.jsonconnected to: 127.0.0.1exported 10951 records

1.3 通过条件导出文档,下面例子导出worth > 100000的文档

$mongoexport -d webmitta -c domain -f "domain,worth" -q '{worth:{$gt:100000}}' -o domain-bk.jsonconnected to: 127.0.0.1exported 10903 records

1.4 使用用户名和密码导出远程服务器的文档

$ mongoexport -h id.mongolab.com:47307 -d heroku_app -c domain -u username123 -p password123 -o domain-bk.jsonconnected to: id.mongolab.com:47307exported 10951 records

注意:导出的文档都是json格式

2. 使用mongoimport命令恢复数据库

相关命令参数:

$ mongoimport --helpconnected to: 127.0.0.1no 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

2.1 从domain-bk.json导入文档,导入的数据库名为webmitta2,集合名为domain2。不存在的数据库或集合会被自动创建。

$ mongoimport -d webmitta2 -c domain2 --file domain-bk.jsonconnected to: 127.0.0.1Wed Apr 10 13:26:12 imported 10903 objects

2.2 导入文档,无则插入,有则更新(根据_id

$ mongoimport -d webmitta2 -c domain2 --file domain-bk.json --upsertconnected to: 127.0.0.1Wed Apr 10 13:26:12 imported 10903 objects

2.3 使用用户名和密码连接远程服务器,并从本地文件domain-bk.json导入文档

$ mongoimport -h id.mongolab.com:47307 -d heroku_app -c domain -u username123 -p password123 --file domain-bk.jsonconnected to: id.mongolab.com:47307Wed Apr 10 13:26:12 imported 10903 objects