我的 Meteor 应用程序获取一个 CSV 文件,使用 Baby Parse(Papa Parse for server)对其进行解析,并将数据插入到 MongoDB 集合中。
每个 CSV 行都作为文档插入。 24 MB CSV 文件包含约 900,000 行;因此,集合中有约 900,000 个文档。每个文档有 5 个字段,包括文档的唯一 ID。
当我使用 dataSize()
获取集合大小时,我收到数字 230172976;如果我没记错的话,这个数字是以字节为单位的;因此它是 230 MB。
为什么会出现如此巨大的增长?我该如何解决这个问题?
最佳答案
这是因为返回的值是 .dataSize()
包括记录padding .另请注意,如果您的文档没有 _id
字段,它将被添加,并且每个 _id
字段都是 12 字节
。您可能想阅读 Record Allocation Strategies
How can I fix this:
使用 collMod带有 noPadding 标志或 db.createCollection()
的命令使用 noPadding 的方法选项。 但你不应该这样做,因为正如 documentation 中所述:
Only set noPadding to true for collections whose workloads have no update operations that cause documents to grow, such as for collections with workloads that are insert-only.
正如 Pete Garafano 在 comment 中提到的那样下面,这适用于 MMAPv1 Storage Engine只要;这是 MongoDB 3.0 和所有以前版本中的默认存储引擎。
MongoDB 3.2 使用 WiredTiger Storage Engine您需要更改默认存储引擎才能在 configuration file 中使用该选项或使用 --storageEngine
选项。
关于mongodb - 为什么 24 MB 的 CSV 数据在 MongoDB 集合中变成了 230 MB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34455028/