相信很多新接触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 exitverbosity 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 outputconnection 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 libraryauthentication 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 usenamespace options:-d, --db=<database-name>                        database to use-c, --collection=<collection-name>              collection to useinput 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

-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

mongodb中文件导入报错error inserting documents解决方法相关推荐

  1. mysql source导入报错ERROR 1366的解决方法

    mysql source导入报错ERROR 1366的解决方法 参考文章: (1)mysql source导入报错ERROR 1366的解决方法 (2)https://www.cnblogs.com/ ...

  2. mysql导入1366_mysql source导入报错ERROR 1366的解决方法

    文件是utf8的,数据库表是utf8的,为什么客户端导入会报错呢? 发现客户端用的是gbk的 改为utf8后正常 SHOW VARIABLES LIKE 'character%'; +-------- ...

  3. mysql启动报错2002_mysql登陆启动报错 ERROR 2002 (HY000) 解决方法

    报错信息 登陆数据库报错:` ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysq ...

  4. 【常见 Error Bug】Vivado仿真报错 ERROR: [XSIM 43-3322] 解决方法

    问题发现 在进行 DDR3 实验时,仿真时出现以下报错信息. ERROR: [XSIM 43-3322] Static elaboration of top level Verilog design ...

  5. 安装32位mysql报错_在CentOS中安装32位或64位MySql报错error: Failed dependencies解决办法...

    在CentOS中安装MySql报错error: Failed dependencies解决办法 安装64位MySql报错内容如下: error: Failed dependencies: libaio ...

  6. sqlyog for MySQL远程连接的时候报错mysql 1130的解决方法

    通过Navicat for MySQL远程连接的时候报错mysql 1130的解决方法 今天在用远程连接Mysql服务器的数据库,不管怎么弄都是连接不到. 错误代码是1130,ERROR 1130: ...

  7. IDEA双击不报错打不开解决方法

    IDEA双击不报错打不开解决方法 今天破解IDEA时修改Edit Custom VM Option那里出现问题,可能是因为有中文路径的原因,也可能是参数设置错误导致IDEA打不开的问题,没有报错,点击 ...

  8. Oracle 报错 28000原因和解决方法

    Oracle 报错 28000原因和解决方法 使用PLSQL登录Oracle数据库时提示"ORA-28000:这个帐号被锁定". 出现这个问题的原因是:在Oracle databa ...

  9. windows下 python 使用 pip 安装TA-Lib报错的原因及解决方法

    windows下 python 使用 pip 安装TA-Lib报错的原因及解决方法 问题环境 问题描述 问题原因 & 解决办法 问题环境 Windows 10 x64 .python 3.8. ...

最新文章

  1. 【讲人话】Angular如何通过@ViewChildren获取实时渲染的动态DOM节点元素(@ViewChild只能获取静态的固定DOM节点)
  2. matlab怎么输入输出文件,[转载]底层文件输入输出函数
  3. android 是否允许打印debug级别日志的开关_Android学习——日志使用
  4. 使用Android Studio打Andorid apk包的流程
  5. tensorboard 使用教程
  6. axios中POST请求变成OPTIONS处理
  7. mysql客户端修改sqlmode_MySQL修改sql_mode
  8. VX的这一重大变化,你支持嘛?
  9. 但救地球要紧的飞鸽传书
  10. android 防止重复启动app,Android应用开发Android 防止启动页面(SplashActivity)被多次启动...
  11. android 导入modoule_android studio如何创建一个子module并引入主工程
  12. Java 容器源码分析之 TreeMap
  13. 微信红包订单存储架构变迁的最佳实践
  14. 环信小程序 Demo源码发布,让你的小程序聊起来
  15. android布局详解
  16. 阿里云储存OSS(服务端签名后前端直传)
  17. nmn抗衰老是骗局吗,美国nmn骗局,正面解答
  18. 常见的系统架构风格有哪些?各有什么优缺点?
  19. 解决pip3 install waring ‘The script xxx is installed in ‘/home/xxx/bin‘ which is not on PATH‘
  20. lvgl cont(容器)

热门文章

  1. 基于STM32的Flash擦除方式
  2. Android BitmapFactory.decodeResource()方法参数代表什么意思
  3. Javaweb学习之中文乱码问题
  4. 双端队列--------------------------------思维(性质)
  5. python matplotlib设置字体大小_python – Matplotlib表的字体大小
  6. 研华安装Linux系统,在自带显卡GeForce RTX 2070的研华MIC-770工控机上安装Ubuntu18.
  7. Linux服务篇之FTP服务及工具
  8. 计算机管理里怎么改ahci,ahci怎么开启_电脑BIOS修改ahci模式教程 - 系统家园
  9. 代码随想录训练营day27
  10. CentOS7 从零安装NVIDA、CUDA、cuDNN