MongoDB 数据导入 mongoimport

1、概述

使用mongoimport命令可以把指定格式的文件导入到指定集合中。可导入JSON数据以及CSV数据。

2、语法

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. C:\mongo\bin>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.  
说明:

-h:数据库宿主机IP

-u:数据库用户名

-p:数据库密码

-d:数据库名字

-c:集合名字
-f:指明导入的列

3、示例

首先删除emp中的数据。

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. db.emp.remove( );  
导入JSON格式的数据:

导入CSV格式的数据:

说明:

-type:指明要导入的文件格式。

--headerline:指明不导入第一行,csv格式的文件第一行为列名。

-file:指明要导入的文件路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值