Storing null vs not storing the key at all in MongoDB

Indeed you have also a third possibility :
key: "" (empty value)

And you forget a specificity about null value.
Query on
key: null will retrieve you all document where key is null or where key doesn’t exist.

When a query on $exists:false will retrieve only doc where field key doesn’t exist.

To go back to your exact question it depends of you queries and what data represent.
If you need to keep that, by example, a user set a value then unset it, you should keep the field as null or empty. If you dont need, you may remove this field.

Leave a Comment