[英]What is the difference between using '&&' and '||' over a ternary operator ('?' and ':')?
[英]What's the difference between normal update and updating using $set operator?
正常方法:
module.exports = (_id, newInfo) => {
return User.update( {_id} , newInfo);
};
使用$ set运算符:
module.exports = (_id, newInfo) => {
return User.update( {_id} , {$set: newInfo} );
};
由于它说在这里 :
不使用$set
更新,
如果替换对象是文档, 则匹配的文档将 被替换 (如果未设置_id,则_id值除外)
而我们使用$set
,
要仅更新所选字段 ,需要使用$ set运算符。 跟随替换对象将替换作者值,但其他所有内容保持不变。
如果没有现有字段,则$Set
会创建一个新字段
如果该字段不存在,则$ set将添加具有指定值的新字段,前提是该新字段不违反类型约束。 如果为不存在的字段指定点划线路径,$ set将根据需要创建嵌入的文档,以实现到该字段的点划线路径。
https://docs.mongodb.com/manual/reference/operator/update/set/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.