是否有MongoDB最大bson大小可以解决?

时间:2013-10-31 19:40:51

标签: mongodb

我正在处理的文件非常庞大。它从极长的调查(如调查猴)收集用户输入,并将答案存储在mongodb数据库中。

我不出所料地得到以下错误

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 }
});

数据字段的一个示例:

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

2 个答案:

答案 0 :(得分: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上的ensureIndex。

尝试不同的方法,对数据进行基准测试,看看哪些方法适合您。

答案 1 :(得分:0)

您应该使用gridfs。它允许您以块的形式存储文档。这是链接:http://docs.mongodb.org/manual/reference/gridfs/

相关问题