如何使用mongoimport导入json文件 [英] how to use mongoimport to import a json file
本文介绍了如何使用mongoimport导入json文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用mongoimport
通过以下命令行导入json文件:
I'm trying to import a json file using mongoimport
with this command line:
mongoimport --db posts --collection rows --file post_history.json
但它返回此错误:
connected to: localhost
Failed: error processing document #1: invalid character 'N' after object key:value pair
imported 0 documents
文件内容如下:
[
{
"Id" : 1,
"PostHistoryTypeId" : 2,
"PostId" : 1,
"RevisionGUID" : "1e04af17-3bdb-4263-aa46-97ee7fb1b0b6",
"CreationDate" : "2011-06-21 20:19:34",
"UserId" : 9,
"Text" : "My finance and myself are looking for a good Caribbean cruise in October and were wondering which islands are best to see and which Cruise line to take?"
},
{
"Id" : 2,
"PostHistoryTypeId" : 1,
"PostId" : 1,
"RevisionGUID" : "1e04af17-3bdb-4263-aa46-97ee7fb1b0b6",
"CreationDate" : "2011-06-21 20:19:34",
"UserId" : 9,
"Text" : "What's the best Caribbean cruise for October"
},
{
"Id" : 3,
"PostHistoryTypeId" : 3,
"PostId" : 1,
"RevisionGUID" : "1e04af17-3bdb-4263-aa46-97ee7fb1b0b6",
"CreationDate" : "2011-06-21 20:19:34",
"UserId" : 9,
"Text" : "<caribbean><cruising><vacation>"
},
{
"Id" : 4,
"PostHistoryTypeId" : 2,
"PostId" : 2,
"RevisionGUID" : "58500a29-b9ed-4802-9934-c173ce362758",
"CreationDate" : "2011-06-21 20:22:33",
"UserId" : 13,
"Text" : "This was one of our definition questions, but also one that interests me personally: How can I find a guide that will take me safely through the Amazon jungle? I'd love to explore the Amazon but would not attempt it without a guide, at least not the first time. And I'd prefer a guide that wasn't going to ambush me or anything :P"
},
{
"Id" : 5,
"PostHistoryTypeId" : 1,
"PostId" : 2,
"RevisionGUID" : "58500a29-b9ed-4802-9934-c173ce362758",
"CreationDate" : "2011-06-21 20:22:33",
"UserId" : 13,
"Text" : "How can I find a guide that will take me safely through the Amazon jungle?"
}
]
我想念什么?这是正确的命令吗?谢谢!
What am I missing? Is it the right command? Thanks!
更新1 :
正如CodeBird所建议的那样,我已经尝试了以下命令:
UPDATE 1:
As CodeBird suggested I've tried this command:
mongoimport --db posts --collection rows --type json --file post_history.json --jsonArray
它返回此:
connected to: localhost
Failed: error processing document #18875: invalid character 'N' after object key:value pair
imported 10000 documents
推荐答案
您应该使用此命令告诉mongo它是一个json数组
You should use this command to tell mongo it is a json array
mongoimport --db posts --collection rows --file post_history.json --jsonArray
我复制了您的json,并进行了测试:
I copied your json, and tested:
mongoimport --db posts --collection rows --file test.json --jsonArray
2016-01-19T06:18:57.887-0600 connected to: localhost
2016-01-19T06:18:57.906-0600 imported 5 documents
编辑
在更新问题之后,我认为您可能还应该为mongo固定一些特殊字符,例如:
Following your question update, I think you might also have some special characters that should be fixed for mongo for example:
tab = \t
" = \"
\ = \\
这篇关于如何使用mongoimport导入json文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文