Mongodb error solutions

Mongodb error solutions

Mongodb error solutions
Page content

Mongodb 报错案例

案例1

  • command
[root@xxxxx]# /opt/mongodb-3.2.10/bin/mongoimport --host xxxxx --port 20000 --collection user_week_activity --db cake --type json --upsert --file /home/suo.li/result.json -vv
  • error
using upsert fields: [_id]
2021-07-23T17:34:59.801+0800    using 2 decoding workers
2021-07-23T17:34:59.801+0800    using 1 insert workers
2021-07-23T17:34:59.801+0800    filesize: 4611622 bytes
2021-07-23T17:34:59.801+0800    using fields:
2021-07-23T17:34:59.804+0800    connected to: xxxxx:20000
2021-07-23T17:34:59.804+0800    ns: cake.user_week_activity
2021-07-23T17:34:59.804+0800    connected to node type: mongos
2021-07-23T17:34:59.804+0800    using write concern: w='majority', j=false, fsync=false, wtimeout=0
2021-07-23T17:34:59.806+0800    using write concern: w='majority', j=false, fsync=false, wtimeout=0
2021-07-23T17:34:59.806+0800    Failed: error processing document #1: invalid character 'ÿ' looking for beginning of value
2021-07-23T17:34:59.806+0800    imported 0 documents
  • cause

由于result.json是mac保存的, 编码格式为UTF-16 Unicode, 导致mongoimport无法处理某些字符, 报错

  • solution

使用vim新建个文本文件, 将result.json粘贴到新文本文件中
如果result.json太大, 则可以使用vscode类似的工具打开, 再以utf-8编码格式保存即可
如果result.json很TM大, 那可以使用命令iconv -f UTF-16LE -t UTF-8 <filename> -o <new-filename> 转换