MongoDB空字符串值vs空值 [英] MongoDB empty string value vs null value

查看:676
本文介绍了MongoDB空字符串值vs空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MongoDB生产中,如果键的值为空或未提供(可选),则应使用空字符串值还是应将null用作值.

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)使用空字符串和null之间有什么优缺点?

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

2)如果我将value设置为undefined以便从现有文档中删除属性,而让property值为空字符串或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?

谢谢

推荐答案

我认为最好的方法是不确定的,因为我建议不要完全包含此键. Mongo不能作为SQL使用,在SQL中每列必须至少为null.如果您没有价值,只需不包含钥匙.然后,如果您查询所有文档,而该键不存在,它将正常工作,否则将无法正常工作.另外,如果您不使用密钥,则会节省一些磁盘空间.在Mongo中这样做是正确的方法.

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

这篇关于MongoDB空字符串值vs空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆