Answer the question
In order to leave comments, you need to log in
Should mongodb store null values, or ignore them when writing?
Hello! Tell me, please, what is the best way to do it: store null values in the database or ignore them?
For example, we have a Car object { name: null, type:1, color: null, speed: 300, owner: Ivan } (this is pseudo code).
If you save this with ignoring null, then d in the base will be the following:
Car {type: 1, speed: 300, owner: Ivan }
If without ignoring null, then it will be the same as in the model before writing.
How is it better?
Answer the question
In order to leave comments, you need to log in
Depends on the case.
For example, if they are not specified in the database, then the JS client will receive undefined in the field , not null .
Perhaps this will play a role in the business logic, and something will go wrong.
See for yourself. :)
If you search on an empty field, then null is needed.
If these fields are filled in the future, then you can put null to less inflate the padding factor.
Also with null, objects look "more whole".
It is worth considering another problem - adding a new field to an existing model. As a result, old records in the database will not have a new field until they are updated.
If you use ORM - then everything will be ok in the application. But you should take this into account when building queries.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question