MongoDB 导入数据(CSV文件)

不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗而已。不要惶恐眼前的难关迈不过去,不要担心此刻的付出没有回报,别再花时间等待天降好运。真诚做人,努力做事!你想要的,岁月都会给你。MongoDB 导入数据(CSV文件),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

 

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代码  

MongoDB 导入数据(CSV文件)

  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客户端查看,这样就导入成功了!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/164241.html

(1)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!