MongoDB数据的导入、导出、备份与恢复

1.将mongodb数据库中的数据导出

  命令:mongoexport -h 192.168.86.127 --port 27017 -d music_backup -c qq_all -o qq_all_20170317

  命令:mongoexport -h 192.168.86.108 --port 27017 -d crawler_book_yamaxun -c yamaxun_comment_info -f _id -q '{"_id":{"$exists":true}}'  --csv -o 123

  导出工具:mongoexport

  1、概念:

     mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件。可以通过参数指定导出的数据项,也可以根据指定的条件导出数据。

  2、语法:

     mongoexport  -h host --port port  -d dbName -c collName  -f field --type json/csv  -o file 

  参数说明:
     -h : 主机
    --port :端口号
            -d :数据库名
            -c :表名
            -o :输出的文件名
            --type : 输出的格式,默认为json
            -f :输出的字段,如果-type为csv,则需要加上-f "字段名"
    -q :输出查询条件
2.将数据导入mongodb
 
  命令:mongoimport -h 192.168.86.126 --port 27017 -d crawler_book_yamaxun -c yamaxun_comment_info --type csv --headerline -f _id --file 123
 
  导入工具:mongoimport 
 
  1、语法:
 
          mongoimport  -h host --port port -d dbName -c collName   --headerline --type json/csv -f field --file filename
 
  参数说明: 
 
     -h : 主机
    --port :端口号
            -d :数据库名
            -c :表名
            --type :导入的格式默认json
            -f :导入的字段名
            --headerline :如果导入的格式是csv,则可以使用第一行的标题作为导入的字段
            --file :要导入的文件
 
3.将数据库备份
  mongodump  --help
  命令:mongodump -h 192.168.86.159 --port 27017 -d weixin -c account_list --forceTableScan
  命令:mongodump -h 192.168.86.160 --port 27017 -d crawler_qq -c album --forceTableScan
 
4.将备份数据导入数据库
  mongorestore --help
  命令:mongorestore -h 192.168.86.160 --port 27017 --dir dump/
  命令:mongorestore -h 192.168.86.160 --port 27017 -d crawler_qq_2514 -c artist --dir dump/crawler_qq/artist.bson
 
原文地址:https://www.cnblogs.com/lingwang3/p/6567857.html