29

我正在尝试导入有效的 MongoDB 70 mb json 文件。但是,我在一个循环中一遍又一遍地收到这个错误:

 01 11:42:20 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: "name": "L
 01 11:42:20
 01 11:42:20 Assertion: 10340:Failure parsing JSON string near: "link": "h
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 kernel32.dll       BaseThreadInitThunk+0x12
 01 11:42:20 ntdll.dll          RtlInitializeExceptionChain+0xef
 01 11:42:20 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: "link": "h
 01 11:42:20
 01 11:42:20 Assertion: 10340:Failure parsing JSON string near: }
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 kernel32.dll       BaseThreadInitThunk+0x12
 01 11:42:20 ntdll.dll          RtlInitializeExceptionChain+0xef
 01 11:42:20 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: }
 01 11:42:20
 01 11:42:20 Assertion: 10340:Failure parsing JSON string near: ],
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 mongoimport.exe    ???
 01 11:42:20 kernel32.dll       BaseThreadInitThunk+0x12
 01 11:42:20 ntdll.dll          RtlInitializeExceptionChain+0xef
 01 11:42:20 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: ],

我的 JSON(只是其中的一个小例子)包含许多类似这样的结构:

[ 
{
   "data": [
         "id": "xxxxxxxxxxxxxxxxxx",
         "from": {
            "name": "yyyyyyyyyyy",
            "id": "1111111111111"
         },
         "to": {
            "data": [
               {
                  "version": 1,
                  "name": "1111111111111",
                  "id": "1111111111111"
               }
            ]
         },
         "picture": "fffffffffffffffffffffff.jpg",
         "link": "http://www.youtube.com/watch?v=qqqqqqqqqqqqq",
         "source": "http://www.youtube.com/v/qqqqqqqqqqqqq?version=3&autohide=1&autoplay=1",
         "name": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
         "description": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...",
         "icon": "http://static.ak.fbcdn.net/rsrc.php/v2/xxx/r/dddd",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.example.com/1111111111111/posts/1111111111111"
            },
            {
               "name": "Like",
               "link": "http://www.example.com/1111111111111/posts/1111111111111"
            }
         ],
         "privacy": {
            "value": ""
         },
         "type": "video",
         "created_time": 1356953890,
         "updated_time": 1356953890,
         "likes": {
            "data": [
               {
                  "name": "jjj ",
                  "id": "59xxx67"
               },
               {
                  "name": "xxxxx",
                  "id": "79xxx27"
               }
            ],
            "count": 2
         },
         "comments": {
            "count": 0
         }
      },

....
....
....
}
]

这是 json 的一般模式“:

[
{
   "data": [
      {

      }
    ],
    "paging": {
      "previous": "link",
      "next": "link"
   }
},
   "data": [
      {
      }
    ],
    "paging": {
      "previous": "link",
      "next": "link"
   }
},
"data": [
      {
      }
    ],
    "paging": {
      "previous": "link",
      "next": "link"
   }
}
]
4

5 回答 5

78

而不是使用:

mongoimport -d DATABASE_NAME -c COLLECTION_NAME --file YOUR_JSON_FILE

使用以下命令:

mongoimport -d DATABASE_NAME -c COLLECTION_NAME --file YOUR_JSON_FILE --jsonArray
于 2013-02-20T07:04:52.420 回答
14

就我而言,我的文件实际上并不太大,因此错误消息具有误导性。我必须将每个文档放在一行中或使用--jsonArray.

所以要么改变文件,如:

{ "_id" : "xxxxxxxx", "foo" : "yyy", "bar" : "zzz" }

或将导入命令更改为

mongoimport -d [db_name] -c [col_name] --file [file_with_multi_lined_docs] --jsonArray

如果我的文件保持在每文档多行格式

{
    "_id" : "xxxxxxxx", 
    "foo" : "yyy", 
    "bar" : "zzz" 
}
于 2013-05-08T16:03:32.667 回答
4

您的 json 文件是否仅包含data字段中的记录列表?在这种情况下,您需要将 json 文件重新格式化为记录列表,如下所示:

     {
     "id": "xxxxxxxxxxxxxxxxxx",
     "from": {
        "name": "yyyyyyyyyyy",
        "id": "1111111111111"
     },
     "to": {
        "data": [
           {
              "version": 1,
              "name": "1111111111111",
              "id": "1111111111111"
           }
        ]
     },
     ......
     }
     {
     "id": "xxxxxxxxxxxxxxxxxx",
     "from": {
        "name": "yyyyyyyyyyy",
        "id": "1111111111111"
     },
     "to": {
        "data": [
           {
              "version": 1,
              "name": "1111111111111",
              "id": "1111111111111"
           }
        ]
     },
     ......
     }

如果您的 json 文件格式正确,只需编辑几行前导/结束行就足够了。

编辑:您可能需要--jsonArray从有效的 json 文件中导入的选项。尝试

mongoimport --db DATABASE_NAME --collection COLLECTION_NAME --file YOUR_JSON_FILE --jsonArray
于 2013-01-01T11:48:16.533 回答
0

In my case, when faced with this error, I found that the problem was from copying and pasting the data from my local machine (in vim) to my remote server (also vim). When I scp'd it across instead it miraculously worked, so no idea what was being changed when copying it across, especially as it was copying from Mac to Centos, so no line ending issues.

In conclusion: use the file that mongoexport creates, not its contents!

于 2014-12-16T13:20:44.353 回答
0

检查 mongo 的版本,我遇到了从 mongo 2.6.1 导出 json 并尝试在 mongo ~2.4 上导入的问题,在安装了更高版本后它对我有用......

于 2014-10-21T09:10:00.237 回答