MongoDB 导入数据(CSV文件)

 

1.用cd命令进入mongodb的bin目录,执行以下命令:./mongo 127.0.0.1:1000

[app@develop bin]$ ./mongo 127.0.0.1:1000

MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:1000/test
> use admin
switched to db admin
> show dbs
APPDB  0.078GB
admin  0.078GB
local  0.078GB
> use APPDB
switched to db APPDB
> show collections
T_TypeInfo
T_Info
system.indexes
> db.T_TypeInfo.count()
125
> db.T_TypeInfo.drop()
true
> show collections
T_Info
system.indexes
> db.createCollection('T_TypeInfo')
{ "ok" : 1 }
> db.T_TypeInfo.count()
0

> exit

清空Collections可以用命令:db.T_TypeInfo.remove({})

2. 回到mongodb的文件目录

[app@develop app]$ ls

> mongodb-linux-x86_64-rhel62-3.0.4 T_TypeInfo_0113.csv

2.1 无用户和密码,执行导入操作命令:

> ./mongodb-linux-x86_64-rhel62-3.0.4/bin/mongoimport -h 127.0.0.1:1000 -d APPDB -c T_TypeInfo --type csv --headerline --file T_TypeInfo_0113.csv

2.2 有用户和密码,先看看mongoimport 相关help:

Shell代码  

  1. [root@localhost mongodb]# ./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.   --version               print the program's version and exit  
  7.   -h [ --host ] arg       mongo host to connect to ( <set name>/s1,s2 for sets)  
  8.   --port arg              server port. Can also use --host hostname:port  
  9.   --ipv6                  enable IPv6 support (disabled by default)  
  10.   -u [ --username ] arg   username  
  11.   -p [ --password ] arg   password  
  12.   --dbpath arg            directly access mongod database files in the given   
  13.                           path, instead of connecting to a mongod  server -   
  14.                           needs to lock the data directory, so cannot be used   
  15.                           if a mongod is currently accessing the same path  
  16.   --directoryperdb        if dbpath specified, each db is in a separate   
  17.                           directory  
  18.   --journal               enable journaling  
  19.   -d [ --db ] arg         database to use  
  20.   -c [ --collection ] arg collection to use (some commands)  
  21.   -f [ --fields ] arg     comma separated list of field names e.g. -f name,age  
  22.   --fieldFile arg         file with fields names - 1 per line  
  23.   --ignoreBlanks          if given, empty fields in csv and tsv will be ignored  
  24.   --type arg              type of file to import.  default: json (json,csv,tsv)  
  25.   --file arg              file to import from; if not specified stdin is used  
  26.   --drop                  drop collection first   
  27.   --headerline            CSV,TSV only - use first line as headers  
  28.   --upsert                insert or update objects that already exist  
  29.   --upsertFields arg      comma-separated fields for the query part of the   
  30.                           upsert. You should make sure this is indexed  
  31.   --stopOnError           stop importing at first error rather than continuing  
  32.   --jsonArray             load a json array, not one item per line. Currently   
  33.                           limited to 4MB.  

参数说明:

-h:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导入那些列

有用户和密码,执行导入操作命令:

> ./mongodb-linux-x86_64-rhel62-3.0.4/bin/mongoimport -h 127.0.0.1:1000 -u test -p test -d APPDB -c T_TypeInfo --type csv --headerline --file T_TypeInfo_0113.csv

导入成功之后出现如下信息:

2016-01-13T15:32:13.921+0800connected to: 127.0.0.1:1000
2016-01-13T15:32:13.950+0800 imported 153 documents

检查是否导入成功,用cd命令进入mongodb的bin目录

[app@develop bin]$ ./mongo 127.0.0.1:1000

MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:1000/test
> use admin
switched to db admin
> show dbs
APPDB  0.078GB
admin  0.078GB
local  0.078GB
> use APPDB
switched to db APPDB
> show collections
T_TypeInfo
T_Info
system.indexes
> db.T_TypeInfo.count()
153

发现count之后的数据也是153, 也可以用db.T_TypeInfo.find()命令查看,或者用MongoVUE客户端查看,这样就导入成功了!

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web3&Basketball

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值