mongo导入导出

来源:互联网 发布:网络流行语用文言文 编辑:程序博客网 时间:2024/05/21 22:21

MongoDB的bin目录下提供了一个mongoexport.exe的程序,可以用于导出数据。

使用$ mongoexport --help 可以查看相关参数的说明:

$ mongoexport --help
Export mongodb data to CSV, TSV or JSON files.

Options:
  --help                                produce help message
 --quiet                               silence all non error diagnostic  messages 
  -h [ --host ] arg                     mongo host to connect to ( <set name>/s1,s2 for sets)  主机名 默认127.0.0.1
  --port arg                            server port. Can also use --host hostname:port  端口 默认27017
  -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 字段名,导出到csv时候需要
  --fieldFile arg                       file with field names - 1 per line
  --csv                                 export to csv instead of json  导出csv格式
  -o [ --out ] arg                      output file; if not specified, stdout  is used 导出文件名

  -q [ --query ] arg                    query filter, as a JSON string, e.g., '{x:{$gt:1}}'  查询条件,使用json格式

  --skip arg (=0)                       documents to skip, default 0  跳过数据,偏移,默认为0
  --limit arg (=0)                      limit the numbers of documents returned, default all 限制返回的documents数量,默认为0

  --sort arg                            sort order, as a JSON string, e.g.,  '{x:1}' 排序


mongoexport -port 2717 -u cache -p ffcsict389 --csv -f Nme,_id,fCd,fLt,fcr -d cache -c t_function -o  /home/mongo/mongodb/cache_date_file/t_function.csv
mongoexport -port 2717 -u cache -p ffcsict389 --csv -f Nme,_id,fCd,fLt,fcr,servid -d cache -c t_function_cur -o  /home/mongo/mongodb/cache_date_file/t_function_cur.csv
mongoexport -port 2717 -u cache -p ffcsict389 --csv -f _id,msgid,msgn,url -d cache -c t_msgdic -o  /home/mongo/mongodb/cache_date_file/t_msgdic.csv
mongoexport -port 2717 -u cache -p ffcsict389 --csv -f _id,Nme,Pwd,fLt,fcr,cId,sQN,pQN,nme,pwd -d cache -c t_platform -o  /home/mongo/mongodb/cache_date_file/t_platform.csv




 进入mongo bin目录执行:./mongoimport -h localhost:2717  -d imp -c enpoints  --type csv --headerline  -file /home/WsxcdE1/files/inmongo3.csv 
   -h 连接地址,如果默认27017端口则不用
   -d 数据库
   -c 表名
   --type 文件类型
   --headerline 指明第一行是列名,不需要导入
   -file 文件路径