[英]mongoDB - update document using $set and a object
I have an object which contains multiple values from input fields. 我有一个对象,其中包含来自输入字段的多个值。 Now I want to update a specific collection. 现在,我想更新一个特定的集合。 I did it like this: 我这样做是这样的:
var info = {age: t.find('input[name=age]').value, organization: t.find('input[name=organization]').value};
Users.update({_id: userId}, {$set: {profile: info}});
Now, the problem is that this removes profile
values which exist before the update operation. 现在,问题在于这将删除更新操作之前存在的profile
值。 Is it possible to keep values in the document, which are not part of the info
object? 是否可以将不属于info
对象的值保留在文档中?
Any help would be greatly appreciated. 任何帮助将不胜感激。
You can use dot notation in the $set
keys to update individual fields in embedded objects: 您可以在$set
键中使用点符号来更新嵌入对象中的各个字段:
var set = {
'profile.info.age': t.find('input[name=age]').value,
'profile.info.organization': t.find('input[name=organization]').value
};
Users.update({_id: userId}, {$set: set});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.