当前位置:   article > 正文

MongoDB 导入导出_mongodb 导出数据库

mongodb 导出数据库

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

1. 使用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

$ mongoexport -d webmitta -c domain -o domain-bk.json
connected to: 127.0.0.1
exported 10951 records
  • 1
  • 2
  • 3

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

$ mongoexport -d webmitta -c domain -f "domain,worth" -o domain-bk.json
connected to: 127.0.0.1
exported 10951 records
  • 1
  • 2
  • 3

1.3 通过条件导出文档,下面例子导出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
  • 1
  • 2
  • 3

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

$ 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
  • 1
  • 2
  • 3

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

2. 使用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2.1 从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
  • 1
  • 2
  • 3

2.2 导入文档,无则插入,有则更新(根据_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
  • 1
  • 2
  • 3

2.3 使用用户名和密码连接远程服务器,并从本地文件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
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/814775
推荐阅读
相关标签
  

闽ICP备14008679号