2017-10-14 140 views
0

我小说的集合,如下所示:MongoDB的文件大小限制

enter image description here

的词数组包含的所有单词,涉及到每个单词的其他语言信息。当我尝试添加较长的文本(10万个字+),我得到的错误:

的RangeError:尝试写入外缓冲区范围

其中,我已经收集,意味着该BSON文件大于16 MB因此超过了限制。

我假设这是一个比较常见的情况。我现在正在考虑如何解决这个限制 - 例如,我可以将这部小说分成10k字的各种大小。或者这是否意味着文档应该组成一个单独的集合(即每个文本上传一个新集合) - 这对我来说是最不合适的。

在这种情况下,是否有标准/建议的方法来设计MongoDB数据库?

另外,是否有可能在JS/Node中插入文档之前检查BSON的大小?

回答

0

您是否绝对需要将书籍的内容存储在MongoDB中?如果您只是向用户提供内容或批量处理内容,我建议将它们存储在磁盘或AWS S3存储桶或类似存储中。

如果你需要的书内容住在数据库,请尝试使用MongoDB的GridFS的:

GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16 MB.

Instead of storing a file in a single document, GridFS divides the file into parts, or chunks, and stores each chunk as a separate document

When you query GridFS for a file, the driver will reassemble the chunks as needed. You can perform range queries on files stored through GridFS. You can also access information from arbitrary sections of files, such as to “skip” to the middle of a video or audio file.

在这里阅读更多: https://docs.mongodb.com/manual/core/gridfs/