json Mongodb Mongoimport 太大:失败解析错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14109474/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:41:31  来源:igfitidea点击:

Mongodb Mongoimport too large: Failure parsing errors

jsonmongodbimport

提问by user63898

I am trying to import into MongoDB 70 mb json file that is valid. But, I am getting this error over and over in a loop:

我正在尝试导入 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: ],

My JSON ( only small example from it ) consist many many structures like this one :

我的 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
         }
      },

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

this is the general patern of the json ":

这是json“的一般模式:

[
{
   "data": [
      {

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

回答by Rajkumar Singh

Instead of using:

而不是使用:

mongoimport -d DATABASE_NAME -c COLLECTION_NAME --file YOUR_JSON_FILE

use the following command:

使用以下命令:

mongoimport -d DATABASE_NAME -c COLLECTION_NAME --file YOUR_JSON_FILE --jsonArray

回答by HaoQi Li

In my case my file was actually not too large, so the error message was misleading. I had to either put each document in a single line or use --jsonArray.

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

So either change the file like:

因此,要么更改文件,如:

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

or change the import command to

或将导入命令更改为

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

if my file stays in the multiple-lines-per-document format

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

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

回答by Chunliang Lyu

Is your json file only contains a list of records in the datafield? In that case, you need to re-format your json file as a list of records like the following:

您的 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"
           }
        ]
     },
     ......
     }

Just edit the a few leading/ending lines should be enough if your json file is well formatted.

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

EDIT: You may need the --jsonArrayoption to import from a valid json file. try

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

mongoimport --db DATABASE_NAME --collection COLLECTION_NAME --file YOUR_JSON_FILE --jsonArray

回答by mickey

Check the version of mongo, I had the problem that I exported json from mongo 2.6.1 and tried to import on a mongo ~2.4, after installing a higher version it worked for me...

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

回答by Matt Fletcher

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.

就我而言,当遇到此错误时,我发现问题出在将数据从我的本地机器(在 vim 中)复制并粘贴到我的远程服务器(也是 vim)。当我scp将它复制过来时,它奇迹般地起作用了,所以不知道复制它时发生了什么变化,尤其是当它从 Mac 复制到 Centos 时,所以没有行结束问题。

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

总之:使用mongoexport创建的文件,而不是它的内容!