Reputation: 85046
Say I have a document like this:
{
email: "[email protected]",
geo:
{
lat: null,
lon: null,
city: null,
state: null,
zip: null
}
}
I have heard that it is sometimes better to "pad" empty values that are likely to be populated later, because then the document will have the correct amount of space allocated to it and will not need to be moved to a larger space if the document grows.
Is it better to set geo
to null
or am I correct to include the empty geo object in case it get's populated in the future?
Upvotes: 0
Views: 216
Reputation: 69663
Padding can be a good idea, but not the way you do it.
MongoDB is a dynamically typed datastore. That means each field will only take as much size as the length of the key plus the length of the value. A BSON nul
value only takes up a single byte (see the BSON specification for more information). That means you are reserving space for the keys but not for the values. When you later replace the null
s with actual data, that data will likely be much larger. This means your document will still grow and will likely still exceed the reserved space triggering a reallocation.
To pad effectively, pad with values which are as large as your expected data.
When you need some way to tell initialized and non-initialized documents apart, consider to not add the real keys, but instead add a single key _reserved: " "
where the number of characters in the dummy-string is the expected length of the keys and values you want to add.
Upvotes: 2