import引入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

but it returns this error:

connected to: localhost

Failed: error processing document #1: invalid character 'N' after object key:value pair

imported 0 documents

The content of the file looks like this:

[

{

"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" : ""

},

{

"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!

UPDATE 1:

As CodeBird suggested I've tried this command:

mongoimport --db posts --collection rows --type json --file post_history.json --jsonArray

it returns this:

connected to: localhost

Failed: error processing document #18875: invalid character 'N' after object key:value pair

imported 10000 documents

解决方案

You should use this command to tell mongo it is a json array

mongoimport --db posts --collection rows --file post_history.json --jsonArray

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

EDIT

Following your question update, I think you might also have some special characters that should be fixed for mongo for example:

tab = \t

" = \"

\ = \\

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值