It seems to me that when you are creating a Mongo document and have a field {key: value}
which is sometimes not going to have a value, you have two options:
- Write
{key: null}
i.e. write null value in the field - Don't store the key in that document at all
Both options are easily queryable, in one you query for {key : null}
and the other you query for {key : {$exists : false}}
.
I can't really think of any differences between the two options that would have any impact in an application scenario (except that option 2 has slightly less storage).
Can anyone tell me if there are any reasons one would prefer either of the two approaches over the other, and why?
EDIT
After asking the question it also occurred to me that indexes may behave differently in the two cases i.e. a sparse index can be created for option 2.