MongoDB empty string value vs null value

前端 未结 3 1238
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 01:17

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 c

3条回答
  •  醉话见心
    2021-02-04 01:48

    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 } 
    });
    

提交回复
热议问题