Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB empty string value vs null value

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

like image 368
Nam Nguyen Avatar asked Sep 04 '13 05:09

Nam Nguyen


1 Answers

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 }  }); 
like image 76
slezadav Avatar answered Sep 28 '22 09:09

slezadav