json - 如何使用mongoimport导入一个json文件

标签 json mongodb database

我正在尝试通过以下命令行使用 mongoimport 导入一个 json 文件:

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?"
 }
]

我错过了什么?这是正确的命令吗?谢谢!

更新 1:
正如 CodeBird 建议的那样,我已经尝试过这个命令:

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数组

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

我复制了你的json,并测试了:

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 修复,例如:

tab = \t
" = \"
\ = \\

关于json - 如何使用mongoimport导入一个json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876544/

相关文章:

mongodb - 如何删除 MongoDB 中的 _id 并替换为另一个字段作为主键?

string - 将字符串添加到 mongodb 中的整个列

mongodb - _rperm 的 parse 中的索引方法

database - 用户为应用程序定义的数据模式

database - 通过命令行运行 Controller 。连接数据库的问题

android - 如何将图像存储在数据库中并在 ListView 中显示

javascript - 如何使用 JSON 或其他序列化库序列化函数?

android - 如何从android中的JSONObject中提取值

javascript - json 返回对象数组,如何为 Angular $scope 变量赋值?

java - jackson- 对象列表 com.fasterxml.jackson.core.io.JsonEOFException : Unexpected end-of-input in field name?