手机版
你好,游客 登录 注册
背景:
阅读新闻

MongoDB基本管理命令

[日期:2012-05-05] 来源:Linux社区  作者:shirdrn [字体:大 中 小]

(五)数据备份、恢复与迁移管理


1、备份全部数据库
mkdir testbak
cd testbak
mongodump
说明:默认备份目录及数据文件格式为./dump/[databasename]/[collectionname].bson
2、备份指定数据库
mongodump -d pagedb
说明:备份数据库pagedb中的数据。
3、备份一个数据库中的某个集合
mongodump -d pagedb -c page
说明:备份数据库pagedb的page集合。
4、恢复全部数据库
cd testbak
mongorestore --drop
说明:将备份的所有数据库恢复到数据库,--drop指定恢复数据之前删除原来数据库数据,否则会造成回复后的数据中数据重复。
5、恢复某个数据库的数据
cd testbak
mongorestore -d pagedb --drop
说明:将备份的pagedb的数据恢复到数据库。
6、恢复某个数据库的某个集合的数据
cd testbak
mongorestore -d pagedb -c page --drop
说明:将备份的pagedb的的page集合的数据恢复到数据库。
7、向MongoDB导入数据
mongoimport -d pagedb -c page --type csv --headerline --drop < csvORtsvFile.csv
说明:将文件csvORtsvFile.csv的数据导入到pagedb数据库的page集合中,使用cvs或tsv文件的列名作为集合的列名。需要注意的是,使用--headerline选项时,只支持csv和tsv文件。
--type支持的类型有三个:csv、tsv、json
其他各个选项的使用,可以查看帮助:
  1. mongoimport --help  
  2. options:  
  3.   --help                  produce help message  
  4.   -v [ --verbose ]        be more verbose (include multiple times for more  
  5.                           verbosity e.g. -vvvvv)  
  6.   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  7.   --port arg              server port. Can also use --host hostname:port  
  8.   --ipv6                  enable IPv6 support (disabled by default)  
  9.   -u [ --username ] arg   username  
  10.   -p [ --password ] arg   password  
  11.   --dbpath arg            directly access mongod database files in the given  
  12.                           path, instead of connecting to a mongod  server -  
  13.                           needs to lock the data directory, so cannot be used  
  14.                           if a mongod is currently accessing the same path  
  15.   --directoryperdb        if dbpath specified, each db is in a separate  
  16.                           directory  
  17.   -d [ --db ] arg         database to use  
  18.   -c [ --collection ] arg collection to use (some commands)  
  19.   -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
  20.   --fieldFile arg         file with fields names - 1 per line  
  21.   --ignoreBlanks          if given, empty fields in csv and tsv will be ignored  
  22.   --type arg              type of file to import.  default: json (json,csv,tsv)  
  23.   --file arg              file to import from; if not specified stdin is used  
  24.   --drop                  drop collection first  
  25.   --headerline            CSV,TSV only - use first line as headers  
  26.   --upsert                insert or update objects that already exist  
  27.   --upsertFields arg      comma-separated fields for the query part of the  
  28.                           upsert. You should make sure this is indexed  
  29.   --stopOnError           stop importing at first error rather than continuing  
  30.   --jsonArray             load a json array, not one item per line. Currently  
  31.                           limited to 4MB.  
8、从向MongoDB导出数据
mongoexport -d pagedb -c page -q {} -f _id,title,url,spiderName,pubDate --csv > pages.csv
说明:将pagedb数据库中page集合的数据导出到pages.csv文件,其中各选项含义:
-f 指定cvs列名为_id,title,url,spiderName,pubDate
-q 指定查询条件
其他各个选项的使用,可以查看帮助:
  1. mongoexport --help  
  2. options:  
  3.   --help                  produce help message  
  4.   -v [ --verbose ]        be more verbose (include multiple times for more verbosity e.g. -vvvvv)  
  5.   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  6.   --port arg              server port. Can also use --host hostname:port  
  7.   --ipv6                  enable IPv6 support (disabled by default)  
  8.   -u [ --username ] arg   username  
  9.   -p [ --password ] arg   password  
  10.   --dbpath arg            directly access mongod database files in the given  
  11.                           path, instead of connecting to a mongod  server -  
  12.                           needs to lock the data directory, so cannot be used  
  13.                           if a mongod is currently accessing the same path  
  14.   --directoryperdb        if dbpath specified, each db is in a separate directory  
  15.   -d [ --db ] arg         database to use  
  16.   -c [ --collection ] arg collection to use (some commands)  
  17.   -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
  18.   --fieldFile arg         file with fields names - 1 per line  
  19.   -q [ --query ] arg      query filter, as a JSON string  
  20.   --csv                   export to csv instead of json  
  21.   -o [ --out ] arg        output file; if not specified, stdout is used  
  22.   --jsonArray             output to a json array rather than one object per line  
注意:
如果上面的选项-q指定一个查询条件,需要使用单引号括起来,如下所示:
  1. mongoexport -d page -c Article -q '{"spiderName": "mafengwoSpider"}' -f _id,title,content,images,publishDate,spiderName,url --jsonArray > mafengwoArticle.txt  
否则,就会出现下面的错误:
  1. ERROR: too many positional options  
linux
相关资讯       MongoDB 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款