当前位置:   article > 正文

mongodb中文件导入报错error inserting documents解决方法_mongodb failed to insert document getlasterror com

mongodb failed to insert document getlasterror command is not support

相信很多新接触mongdb的朋友在导入数据时都或多或少的遇上些许问题,下面就我遇上的问题做个简单记录。

安装完MongoDB会自带个导入工具:mongoimport,导入的指令为

  1. Usage:
  2. mongoimport <options> <file>
  3. Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin.
  4. See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information.
  5. general options:
  6. --help print usage
  7. --version print the tool version and exit
  8. verbosity options:
  9. -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)
  10. --quiet hide all log output
  11. connection options:
  12. -h, --host=<hostname> mongodb host to connect to (setname/host1,host2 for replica sets)
  13. --port=<port> server port (can also use --host hostname:port)
  14. ssl options:
  15. --ssl connect to a mongod or mongos that has ssl enabled
  16. --sslCAFile=<filename> the .pem file containing the root certificate chain from the certificate authority
  17. --sslPEMKeyFile=<filename> the .pem file containing the certificate and key
  18. --sslPEMKeyPassword=<password> the password to decrypt the sslPEMKeyFile, if necessary
  19. --sslCRLFile=<filename> the .pem file containing the certificate revocation list
  20. --sslAllowInvalidCertificates bypass the validation for server certificates
  21. --sslAllowInvalidHostnames bypass the validation for server name
  22. --sslFIPSMode use FIPS mode of the installed openssl library
  23. authentication options:
  24. -u, --username=<username> username for authentication
  25. -p, --password=<password> password for authentication
  26. --authenticationDatabase=<database-name> database that holds the user's credentials
  27. --authenticationMechanism=<mechanism> authentication mechanism to use
  28. namespace options:
  29. -d, --db=<database-name> database to use
  30. -c, --collection=<collection-name> collection to use
  31. input options:
  32. -f, --fields=<field>[,<field>]* comma separated list of field names, e.g. -f name,age
  33. --fieldFile=<filename> file with field names - 1 per line
  34. --file=<filename> file to import from; if not specified, stdin is used
  35. --headerline use first line in input source as the field list (CSV and TSV only)
  36. --jsonArray treat input source as a JSON array
  37. --type=<type> input format to import: json, csv, or tsv (defaults to 'json')
  38. ingest options:
  39. --drop drop collection before inserting documents
  40. --ignoreBlanks ignore fields with empty values in CSV and TSV
  41. --maintainInsertionOrder insert documents in the order of their appearance in the input source
  42. -j, --numInsertionWorkers=<number> number of insert operations to run concurrently (defaults to 1)
  43. --stopOnError stop importing at first insert/upsert error
  44. --upsert insert or update objects that already exist
  45. --upsertFields=<field>[,<field>]* comma-separated fields for the query part of the upsert
  46. --writeConcern=<write-concern-specifier> write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')
  47. --bypassDocumentValidation bypass document validation

-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的导入导出有个博主写得比较详细,附上博文链接:

https://www.iteye.com/blog/chenzhou123520-1641319

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/286949
推荐阅读
相关标签
  

闽ICP备14008679号