Hi @karto_sack
reducing document size by altering json structure is a very interesting topic. I did some experiments (I put links to details in the end of this post) and in a nutshell, here is what is worth considering:
- using short keys - like you said
- dropping unused fields (f.i. if you use Spring - very popular Java framework - it adds
_classfield to every document, and it’s useless unless you use class inheritance) - using proper data types: many developer don’t check how their data is serialized and they don’t know that sometime numbers and dates are serialized as strings which is not optimal in terms of storage size.
- use
ObjectIdinstead of strings - if you have fields that could be used as default value you can omit serializing them, f.i. If 99% of amounts in your DB is for USD currency - just skip this symbol and save only currencies other than USD, the same you can do for flags: if 99% of users have some flag set to
truejust save only those that havefalse- you can save a lot of space - skip serialization of empty fields i.e. avoid:
{someField: null} - consider using flat structure instead of complex types, f.i. instead of
{user: {name:'John', surname: 'Doe'}you can do it like:{usrName: 'John', usrSurname: 'Doe'}- there will be some gain if you have millions of documents
You can find results of my experiments here: Impact of data model on MongoDB database size Series' Articles - DEV Community
Regards,
Michał.