27

In MongoDB production, if a value of a key is empty or not provided (optional), should I use empty string value or should I use null for value.

1) Is there any pros vs cons between using empty string vs null?

2) Is there any pros vs cons if I set value to undefined to remove properties from your existing doc vs letting properties value to either empty string or null?

Thanks

3 Answers 3

19

I think the best way is undefined as I would suggest not including this key altogether. Mongo doesn't work as SQL, where you have to have at least null in every column. If you don't have value, simply don't include the key. Then if you make query for all documents, where this key doesn't exists it will work correctly, otherwise not. Also if you don't use the key you save a little bit of disk space. Do this is the correct way in Mongo.

function deleteEmpty (v) {
   if(v==null){
     return undefined;
   }
   return v;
}

var UserSchema = new Schema({
email: { type: String, set: deleteEmpty } 
});
6
  • in mongo schema, how can you go about to make it that if empty string after trim or null, remove it from document? I know I can put trim: true in schema but I do not know how to auto remove when value is null or empty string. Any thought? Thanks
    – Nam Nguyen
    Commented Sep 4, 2013 at 6:21
  • I don't exactly understand what you want, but try here stackoverflow.com/questions/12636938/…
    – slezadav
    Commented Sep 4, 2013 at 6:53
  • 1
    you know in Mongoose, there is a trim function that we can define in schema to trim value automatically, EX: login: { required: true, trim: true }, and I'm wondering if there is another similar syntax that I can use to auto set value to undefined if value is either empty string or null ?
    – Nam Nguyen
    Commented Sep 4, 2013 at 7:20
  • See edit, it is the only thingthat came to my mind. PS, can't try it now, there might be some errors
    – slezadav
    Commented Sep 4, 2013 at 7:49
  • Note that if you use the trim option with this setter you will get the following error: Cast to string failed for value "undefined" at path "XXXXX"
    – rlay3
    Commented Dec 15, 2014 at 0:28
8

i would say that null indicates absence of the value and empty string indicates that the value is there, but its empty. While reading the data you can distinguish between blank values and non-existing values. Still it depends upon your use-case

0
7

This question has been answered at least 4 times by me and a Google search will get you a lot of information.

You must take into consideration what removing the key means. If your document will eventually use that schema in most of its defined state, within the application, then you could be seeing a lot of movement of the document, this neuts the benefit of no having these keys: space. Those couple of bytes you will save in space will be rendered useless and you will get a swiss cheese effect.

However if you do not use these fields at all then having those few extra bytes with millions of documents in your working set could cause real problems that need not be there (if you for some reason want to shove that many documents into your working set), as for the space issue, MongoDB fundamentally has a space issue and I have not really known omitting a couple of keys to do anything to help that.

5
  • I do use these fields and these fields are optional. Ex, work phone field, some user has work # and some people do not. So from your suggestion, you would recommend to not include key in doc if value is empty or null, this will save space and could improve performance in general.
    – Nam Nguyen
    Commented Sep 4, 2013 at 7:27
  • @NamNguyen If those fields get used consistently and peope fill in their information regularly then you will see a performance downgrade as the information you hold on them grows
    – Sammaye
    Commented Sep 4, 2013 at 7:28
  • i will as this question again as I ask @slezadave above, there is a trim function that we can define in schema to trim value automatically, EX: login: { required: true, trim: true }, and I'm wondering if there is another similar syntax that I can use to auto set value to undefined if value is either empty string or null ?
    – Nam Nguyen
    Commented Sep 4, 2013 at 7:29
  • @NamNguyen This is nothing to do with Mongoose, this is fundamental to how MongoDB allocates disk space and how it uses its padding factor
    – Sammaye
    Commented Sep 4, 2013 at 7:30
  • @NamNguyen Though to actually answer your last comment, I do not know of a function in node I think you may need to build your own event on before save of the model
    – Sammaye
    Commented Sep 4, 2013 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.