总结:减小mongodb存储空间的方法
1.使用_id字段。应为系统不换你用不用都会创建这个字段。
2.是field的名字尽量短。应为这个field名字会存在每个document中。
以下是mongodb帮助文档的原文
How do I optimize storage use for small documents?
Each MongoDB document contains a certain amount of overhead. This overhead is normally insignificant but becomes significant if all documents are just a few bytes, as might be the case if the documents in your collection only have one or two fields.
Consider the following suggestions and strategies for optimizing storage utilization for these collections:
Use the _id field explicitly.
MongoDB clients automatically add an _id field to each document and generate a unique 12-byte ObjectId for the _id field. Furthermore, MongoDB always indexes the _id field. For smaller documents this may account for a significant amount of space.
To optimize storage use, users can specify a value for the _id field explicitly when inserting documents into the collection. This strategy allows applications to store a value in the _id field that would have occupied space in another portion of the document.
You can store any value in the _id field, but because this value serves as a primary key for documents in the collection, it must uniquely identify them. If the field’s value is not unique, then it cannot serve as a primary key as there would be collisions in collection.
Use shorter field names.
MongoDB stores all field names in every document. For most documents, this represents a small fraction of the space used by a document; however, for small documents the field names may represent a proportionally large amount of space. Consider a collection of documents that resemble the following:
{ last_name : "Smith", best_score: 3.9 }
If you shorten the filed named last_name to lname and the field name best_score to score, as follows, you could save 9 bytes per document.
{ lname : "Smith", score : 3.9 }
Shortening field names reduces expressiveness and does not provide considerable benefit on for larger documents and where document overhead is not significant concern. Shorter field names do not reduce the size of indexes, because indexes have a predefined structure.
In general it is not necessary to use short field names.
Embed documents.
In some cases you may want to embed documents in other documents and save on the per-document overhead.
如何优化存储使用较小的文件吗?
每个MongoDB的文档中包含一定量的开销。这方面的负担通常是微不足道的,但变得重要,如果所有文档都只有几个字节,可能的情况下,如果在您的收藏中的文件只能有一个或两个领域。
请考虑以下的建议和策略,优化存储利用率,这些集合:
使用_id领域明确。
MongoDB的客户端自动添加一个_id字段对于每个文档的_id字段生成一个唯一的12字节的ObjectId。此外,MongoDB的索引的_id领域。对于较小的文件,这也许可以解释为一个显着的空间量。
要优化存储的使用,用户可以指定明确的_id字段的值插入到集合中的文件。这个策略允许应用程序存储在_id的值领域,将有另一部分的文件占用的空间在。
您可以存储任何价值在_id领域,但因为这个值作为文件集合中的主键,它必须唯一地标识。如果该字段的值是不是唯一的,那么它不能作为主键,将收集的碰撞。
使用较短的字段名。
MongoDB的存储在每个文档中的所有字段名。对于大多数的文件,这代表的文件所占用的空间的一小部分,但是,对于小文件的字段名可能是一个比例较大的空间量。考虑类似于以下内容的文件的集合:
{LAST_NAME:“史密斯”,best_score 3.9}
如果您缩短所提交的名为姓氏lname的和字段名best_score的得分,如下,你可以保存每个文档的9个字节。
{LNAME:“史密斯”,得分:3.9}
缩短的字段名,降低了表现力和不提供可观的效益更大的文档和文件的开销是不显着的关注。较短的字段名不减少大小的索引,因为索引有一个预定义的结构。
一般来说它是没有必要使用短字段名。
嵌入的文件。
在某些情况下,你可能想在其他文件中嵌入文件,并保存每个文档的开销