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:
- [root@localhost mongodb]# ./bin/mongoimport –help
- options:
- –help produce help message
- -v [ –verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- –version print the program’s version and exit
- -h [ –host ] arg mongo host to connect to ( <set name>/s1,s2 for sets)
- –port arg server port. Can also use –host hostname:port
- –ipv6 enable IPv6 support (disabled by default)
- -u [ –username ] arg username
- -p [ –password ] arg password
- –dbpath arg directly access mongod database files in the given
- path, instead of connecting to a mongod server –
- needs to lock the data directory, so cannot be used
- if a mongod is currently accessing the same path
- –directoryperdb if dbpath specified, each db is in a separate
- directory
- –journal enable journaling
- -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
- –fieldFile arg file with fields names – 1 per line
- –ignoreBlanks if given, empty fields in csv and tsv will be ignored
- –type arg type of file to import. default: json (json,csv,tsv)
- –file arg file to import from; if not specified stdin is used
- –drop drop collection first
- –headerline CSV,TSV only – use first line as headers
- –upsert insert or update objects that already exist
- –upsertFields arg comma-separated fields for the query part of the
- upsert. You should make sure this is indexed
- –stopOnError stop importing at first error rather than continuing
- –jsonArray load a json array, not one item per line. Currently
- 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