2013-03-24 50 views
10

我有几个有效的JSON文档,我尝试在mongodb中导入它们。mongoimport期间出错

{ "Books": 
    [{ "ISBN":"ISBN-0-13-713526-2", 
    "Price":85, 
    "Edition":3, 
    "Title":"A First Course in Database Systems", 
    "Authors":[ {"First_Name":"Jeffrey", "Last_Name":"Ullman"}, 
       {"First_Name":"Jennifer", "Last_Name":"Widom"} ] } 
, 
{ "ISBN":"ISBN-0-13-815504-6", 
    "Price":100, 
    "Remark":"Buy this book bundled with 'A First Course' - a great deal!", 
    "Title":"Database Systems:The Complete Book", 
    "Authors":[ {"First_Name":"Hector", "Last_Name":"Garcia-Molina"}, 
       {"First_Name":"Jeffrey", "Last_Name":"Ullman"}, 
       {"First_Name":"Jennifer", "Last_Name":"Widom"} ] }], 
"Magazines": 
[{ "Title":"National Geographic", 
    "Month":"January", 
    "Year":2009 } 
, 
{ "Title":"Newsweek", 
    "Month":"February", 
    "Year":2009 }, 
{"Title": "Newsweek"}] 
} 

我得到了以下错误:

Sun Mar 24 15:23:27 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: "Month":"J Sun Mar 24

15:23:27 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: "Year":200 Sun Mar 24 15:28:23

ERROR: encountered 28 errors

可能有人说了什么问题?

+1

这是你想导入整个JSON文件?该错误表明你的文件是[超过16MB](http://stackoverflow.com/questions/14109474/mongodb-mongoimport-too-large-failure-parsing-errors)。 – 2013-03-24 14:44:11

+0

这是整个文档,包含888个字节 – damluar 2013-03-24 15:11:57

回答

30

默认情况下,mongoimport预计每个文档都将位于JSON文件的一行中。您需要使用您使用的格式将--jsonArray传递给mongoimport,或者只需确保单个文档的所有数据都位于同一行上。

(我救了你的文件,如,增加了--jsonArray,选择和它工作得很好。)

1

MongoDB每个文档的限制为16 MB。如果你想克服这个限制,你需要使用GridFS。请看文档here

+0

我发布了整个文件,所以它的总共888字节 – damluar 2013-03-24 15:24:56

+0

好吧,现在我看到我认为你有一堆这样的条目。 – Michal 2013-03-24 15:26:14

0

从终端下面的命令为我工作很好:

mongoimport -d my_db -c my_collection < my_json_file.json --batchSize 1 

一些版本需要改变在--batchSize 50,而不是1象下面这样:

mongoimport -d my_db -c my_collection < my_json_file.json --batchSize 50 
0

如果要导入JSON文件,你可以用这个也

也为CSV使用--type csv

mongoimport --db database --collection collection_name filename.json --type json