Mongodb - Mongoimport 错误无效字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30870155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongodb - Mongoimport error invalid character
提问by Ak040
I tried to import a simple json file using mongoimport and i get the following error
我尝试使用 mongoimport 导入一个简单的 json 文件,但出现以下错误
PER-MacBook-Pro:/AJ$ mongoimport --db test --collection samplePM --file /users/AJ/Documents/Development/ETLwork/Dummydata/Penguin_Players.json
2015-06-16T09:53:57.291-0400 connected to: localhost
2015-06-16T09:53:57.293-0400 Failed: error processing document #1: invalid character '\' looking for beginning of object key string
2015-06-16T09:53:57.293-0400 imported 0 documents
Sample json file is as follows:
示例json文件如下:
{
"position":"Right Wing",
"id":8465166,
"weight":200,
"height":"6' 0\"",
"imageUrl":"http://1.cdn.nhle.com/photos/mugs/8465166.jpg",
"birthplace":"Seria, BRN",
"age":37,
"name":"Craig Adams",
"birthdate":"April 26, 1977",
"number":27
},
{
"position":"Right Wing",
"id":8475761,
"weight":195,
"height":"6' 2\"",
"imageUrl":"http://1.cdn.nhle.com/photos/mugs/8475761.jpg",
"birthplace":"Gardena, CA, USA",
"age":23,
"name":"Beau Bennett",
"birthdate":"November 27, 1991",
"number":19
}
Am I doing something wrong here?
我在这里做错了吗?
回答by Ak040
The comment about non "UTF-8" characters was helpful.
关于非“UTF-8”字符的评论很有帮助。
It seems like there is a problem with creating json documents using textedit in Mac. I could not find these non UTF-8 characters but i created the same file using vi test.json in mac shell. I pasted the contents, saved the file and used mongoimport. It works now.
在 Mac 中使用 textedit 创建 json 文档似乎存在问题。我找不到这些非 UTF-8 字符,但我在 mac shell 中使用 vi test.json 创建了相同的文件。我粘贴了内容,保存了文件并使用了 mongoimport。它现在有效。
Thanks
谢谢
回答by Ardhi
I was able to get away with using --jsonArray
tag, giving the file full path, and modified it adding bracket at the beginning and at the end,
我能够摆脱使用--jsonArray
标签,提供文件完整路径,并修改它在开头和结尾添加括号,
[{"my":"json","file":"imported"},{"my":"cool","file":"succeeded"}]
mongoimport --db myCoolDb --collection myCoolColl --file /path/to/my/imported/file.json --jsonArray
回答by Winter
I got the same error while importing json data. Instead use the .bson data using mongorestore command.
我在导入 json 数据时遇到了同样的错误。而是使用 mongorestore 命令使用 .bson 数据。
mongorestore -d <db> -c <collection> <.bson-file>
Use --drop
if you want to drop the existing data in the collection.
使用--drop
,如果你想在集合中丢弃现有的数据。
回答by JP Lew
I was getting Failed: error processing document #112783: invalid character ',' looking for beginning of value
because one of my objects was formatted improperly. Notice how "psychosurgery" is missing curly braces:
我收到Failed: error processing document #112783: invalid character ',' looking for beginning of value
是因为我的一个对象格式不正确。请注意“心理外科”如何缺少花括号:
{
"word": "psychosurgeons",
"firstLetter": "p"
}
" psychosurgery",
{
"word": "psychosurgical",
"firstLetter": "p"
}
Since there are over 600,000 lines in the file I'm trying to import, this would have been tough to find manually.
由于我尝试导入的文件中有超过 600,000 行,因此很难手动找到。
So I ran the same mongoimport
command with full verbosity (-vvvvv
) enabled, and the script stopped right on the problematic object. See mongoimport --help
for more info.
因此,我在启用mongoimport
完整详细度 ( -vvvvv
) 的情况下运行了相同的命令,并且脚本就在有问题的对象上停止了。查看mongoimport --help
更多信息。
Hope this helps someone.
希望这可以帮助某人。
回答by ThomasDeLange
I got the same problem because I used texteditor on the mac. The solution was to convert the file to plain text. Make sure the extension ends in .json because texteditor wants to put .txt at the end.
我遇到了同样的问题,因为我在 mac 上使用了 texteditor。解决方案是将文件转换为纯文本。确保扩展名以 .json 结尾,因为 texteditor 想要将 .txt 放在最后。
回答by Ashiq KS
Just open a text file, copy all the data to the newly created text file. While saving the text file select the option 'UTF-8' in the Encodingdrop down and later change the text file to JSON or CSV by renaming.
只需打开一个文本文件,将所有数据复制到新创建的文本文件中。保存文本文件时,在编码下拉列表中选择“UTF-8”选项,然后通过重命名将文本文件更改为 JSON 或 CSV。
Then import the file as usual as it is.
然后照常导入文件。