2013-10-31 98 views
7

我正在處理的文檔非常大。它從一個非常長的調查中收集用戶輸入(如調查猴子)並將答案存儲在一個mongodb數據庫中。是否有MongoDB最大bson大小?

我意料之中收到以下錯誤

Error: Document exceeds maximal allowed bson size of 16777216 bytes 

如果我不能改變我的文檔中的字段有什麼我可以做什麼?有沒有辦法壓縮文件,通過刪除空白或類似的東西?

編輯

這裏是文檔

Schema({ 
    id : { type: Number, required: true }, 
    created: { type: Date, default: Date.now }, 
    last_modified: { type: Date, default: Date.now }, 
    data : { type: Schema.Types.Mixed, required: true } 
}); 

數據字段的一個例子的結構:你應該使用gridfs

{ 
    id: 65, 
    question: { 
     test: "some questions", 
     answers: [2,5,6] 
    } 
    // there could be thousands of these question objects 
} 
+1

你可以發佈一個文件的例子嗎?只是顯示結構的一個子集,這將有助於診斷。 –

+0

如果沒有一些背景知識,這真的很難說。請給我們一些額外的細節?你需要搜索所有字段的能力嗎?是否需要單個文檔的原子更新? – zero323

+0

@TomSwifty我添加了一些更多的細節。我不需要搜索存儲所有信息的數據字段。 – bejm

回答

6

你可以做的一件事是建立你自己的mongoDB :-)。 Mongodb是一個開放的source,並且對文檔大小的限制是強制執行better schema design的任意。您可以修改this line併爲自己構建。小心這個。

最直接的想法是每個小問題在一個不同的文件與一個字段引用其父。

另一種想法是限制父文件的數量。比方說,你是限制N個元素,則父看起來是這樣的:

{ 
    _id : ObjectId(), 
    id : { type: Number, required: true }, 
    created: { type: Date, default: Date.now }, // you can store it only for the first element 
    last_modified: { type: Date, default: Date.now }, // the same here 
    data : [{ 
    id: 65, 
    question: { 
     test: "some questions", 
     answers: [2,5,6] 
    } 
    }, ... up to N of such things {} 
    ] 
} 

這樣修改次數N,你可以確保你將在16 MB BSON的。並且爲了讀取整個調查,您可以選擇

db.coll.find({id: the Id you need})然後將整個調查結合在應用程序級別上。另外不要忘記在id上確保索引。

嘗試不同的事情,對您的數據做一個基準測試,看看有什麼適合您的。

+1

我想我將不得不與你提到的 - 「在不同文件中的每個小問題」 – bejm

+0

有趣的解決方案的大小限制,雖然「與大國來承擔巨大的責任」! – EmptyArsenal

+0

這實際上不會解決尺寸問題,因爲數據字段實際上比blob格式要大,解決方法是將問題歸因於問題集合 – Sammaye

相關問題