3

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.

Jamie S
  • 649
  • 6
  • 9
  • if the field will likely be set in most documents in the future there is value to creating it from the start with a "dummy" string to represent unset value and hold allocated space for this value (so adding it in the future won't grow the document possibly causing collection fragmentation). – Asya Kamsky Nov 06 '12 at 19:37
  • Mongo adds some buffer around each document by default to avoid fragmentation. If you still suggest to predefine the field then better use the native type null to specify the absence of a value – Aravind Yarram Nov 07 '12 at 00:40

2 Answers2

4

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.

Community
  • 1
  • 1
Brian Cajes
  • 3,274
  • 3
  • 21
  • 22
1

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?

Community
  • 1
  • 1
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • Thanks for letting me know about the $exists operator, and I'll definitely be using null instead of empty strings. – Jamie S Nov 07 '12 at 17:53