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

bej*_*ejm 7 mongodb

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

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

Error: Document exceeds maximal allowed bson size of 16777216 bytes
Run Code Online (Sandbox Code Playgroud)

如果我无法更改文档中的字段,我还能做些什么吗?有没有办法压缩文档,删除空格或类似的东西?

编辑

这是文档的结构

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 }
});
Run Code Online (Sandbox Code Playgroud)

数据字段的示例:

{
    id: 65,
    question: {
        test: "some questions",
        answers: [2,5,6]
    }
    // there could be thousands of these question objects
}
Run Code Online (Sandbox Code Playgroud)

Sal*_*ali 6

你可以做的一件事是建立自己的mongoDB :-).Mongodb是一个开源的,对文档大小的限制是相当随意的,以强制执行更好的模式设计.您可以修改此行并自行构建.小心这个.

最直接的想法是将每个小问题放在一个不同的文档中,并带有一个引用其父级的字段.

另一个想法是限制父项中的文档数量.假设您限制为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 {}
  ]
}
Run Code Online (Sandbox Code Playgroud)

这样修改数字N就可以确保你将使用16 MB的BSON.并且为了阅读整个调查,您可以选择

db.coll.find({id: the Id you need})然后将整个调查结合在应用程序级别上.另外不要忘记保证索引id.

尝试不同的事情,对您的数据进行基准测试,看看哪些对您有用.