Jamie S
Jamie S

Reputation: 679

MongoDB documents: empty value or omit pair altogether?

I'm using MongoDB for the first time on a project, and I'm not quite sure what is the recommended approach for blank/unset values in a document. Which of these two approaches is more appropriate when you have pairs that will likely have values in the future:

1) The JSON where the description field is an empty string (and will be populated in the future):

{
    "username": "jamies",
    "shortName": "camping",
    "setName": "Camping on Stevens",
    "description": ""
}

2) Or, the JSON where the description field is omitted (and will be added in the future):

{
    "username": "jamies",
    "shortName": "camping",
    "setName": "Camping on Stevens"
}

Obviously the 2nd approach will save disk space, but also doesn't indicate what values are null. I'd love to understand which is recommended/prescribed by Mongo developers/DBAs, and if there are any considerations with that approach during query or update.

Upvotes: 3

Views: 2370

Answers (2)

Brian Cajes
Brian Cajes

Reputation: 3402

When in doubt

Start with brevity. Increase the other dimensions as required by testing. jeff-atwood

The quote above refers to code, but it applies here, as well. I can think of a few situations where you'll regret adding an empty string field in the future. eg. Deprecating the field, it not being in use anymore, confusing people, and just taking up space. Also, refer to MongoDB Docs: Querying and nulls.

Upvotes: 4

Aravind Yarram
Aravind Yarram

Reputation: 80176

One way is to ignore the fields that are not applicable for the document/aggregate under question. Document data bases like MongoDB are schema less and support this natively. You can then use the $exists query operator. But if your use-case is heavy on the queries that depend upon exists then it is non-performant and alternate designs are mentioned.

Another way is to use null to indicate the absence of a value. I think using null is suggested instead of adding a element and providing empty string as the value. Somewhat similar discussion here XML, what is this: null or empty element?

Upvotes: 1

Related Questions