赞
踩
相信很多新接触mongdb的朋友在导入数据时都或多或少的遇上些许问题,下面就我遇上的问题做个简单记录。
安装完MongoDB会自带个导入工具:mongoimport,导入的指令为
- Usage:
- mongoimport <options> <file>
-
- Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
-
- See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.
-
- general options:
- --help print usage
- --version print the tool version and exit
-
- verbosity options:
- -v, --verbose=<level> more detailed log output (include multiple times for more verbosity, e.g. -vvvvv, or specify a numeric value, e.g. --verbose=N)
- --quiet hide all log output
-
- connection options:
- -h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
- --port=<port> server port (can also use --host hostname:port)
-
- ssl options:
- --ssl connect to a mongod or mongos that has ssl enabled
- --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
- --sslPEMKeyFile=<filename> the .pem file containing the certificate and key
- --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
- --sslCRLFile=<filename> the .pem file containing the certificate revocation list
- --sslAllowInvalidCertificates bypass the validation for server certificates
- --sslAllowInvalidHostnames bypass the validation for server name
- --sslFIPSMode use FIPS mode of the installed openssl library
-
- authentication options:
- -u, --username=<username> username for authentication
- -p, --password=<password> password for authentication
- --authenticationDatabase=<database-name> database that holds the user's credentials
- --authenticationMechanism=<mechanism> authentication mechanism to use
- namespace options:
- -d, --db=<database-name> database to use
- -c, --collection=<collection-name> collection to use
- input options:
- -f, --fields=<field>[,<field>]* comma separated list of field names, e.g. -f name,age
- --fieldFile=<filename> file with field names - 1 per line
- --file=<filename> file to import from; if not specified, stdin is used
- --headerline use first line in input source as the field list (CSV and TSV only)
- --jsonArray treat input source as a JSON array
- --type=<type> input format to import: json, csv, or tsv (defaults to 'json')
- ingest options:
- --drop drop collection before inserting documents
- --ignoreBlanks ignore fields with empty values in CSV and TSV
- --maintainInsertionOrder insert documents in the order of their appearance in the input source
- -j, --numInsertionWorkers=<number> number of insert operations to run concurrently (defaults to 1)
- --stopOnError stop importing at first insert/upsert error
- --upsert insert or update objects that already exist
- --upsertFields=<field>[,<field>]* comma-separated fields for the query part of the upsert
- --writeConcern=<write-concern-specifier> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')
- --bypassDocumentValidation bypass document validation
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
例如现在MongoDB中有个库名为admin,admin数据库下有个collection名为mongoTest,此时我们要导入的文件为:/home/mongo目录下的mongoData.csv,网上大部分的教程的导入指令为:
[root@localhost ~]# mongoimport -d admin -c mongoTest /home/mongo/mongoData.csv
直接执行该指令为报如下错误:
error inserting documents: not authorized on admin to execute command { insert: "T_ALERT_HOME", documents: 2, writeConcern: { getLastError: 1, w: 1 }, ordered: false }
网上关于这个错误的解决方法说得大都不清晰,而且说的基本都是重复的,导致我卡了很多时间,最后才解决。
出现该报错的原因是由于我们要把数据插入到admin库,没有权限。相信很多朋友都会纳闷,在此之前明明已经给admin库或设置了readWrite权限甚至是root权限(一般设置的用户名称都是admin,
密码为123456),怎么可能会没有权限执行插入呢?
经过我的测试,原来是在导入时,MongoDB会对操作的用户进行检查,此时我们要把数据导入到admin库中,我们需要带上有权限的的用户名和密码(猜测相当于账号验证db.auth("admin","123456")),
所以最后我们执行的导入语句应该如下:
[root@localhost ~]# mongoimport -u admin -p 123456 -d admin -c mongoTest /home/mongo/mongoData.csv
执行成功结果如下:
进入到mongo数据库admin,查看mongoTest集合的数据,查看命令:
db.mongoTest.find().pretty()【加入pretty()是指定用json格式来显示数据,方便查看】
查看总数:db.mongoTest.find().count();
如果出现你导入的数据,即证明导入成功
总算解决了,留下该问题的记录,希望能帮到后面的同学,有问题可以留言,看到会回。
mongo的导入导出有个博主写得比较详细,附上博文链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。