![](/img/trans.png)
[英]Meteor.js & Mongo: can you insert a new document or parameter into the user.profile from the client?
[英]Update user.profile with dynamic object in Meteor
所以,我想要实现的是更新user.profile
使旧的未更新数据已经存在于user.profile
。
因此,我的初始user.profile
具有以下内容:
{
accountType: 'Student',
xyz: 'something',
etc...
}
在我的更新方法中,如果不需要更新,我想保留这些值,因此,如果要添加以下内容:
{
'xyz': 'something else',
'bar': 'bar',
etc...
}
我希望看到具有合并和更新的对象的更新的配置文件。
我尝试使用的是update
和upsert
但是在两种情况下以及我尝试更新user.profile
时的所有测试中,旧数据都被新数据完全替换了...
这是我最近的尝试之一:
Meteor.users.update(this.userId, {
$set: {
profile: data
}
},
{ upsert: true });
但我也尝试过:
Meteor.users.upsert(this.userId, {
$set: {
profile: data
}
});
我怎样才能达到我所需要的? 谢谢
从Mongo文档中 :
$ set运算符用指定的值替换字段的值。
因此,当您将其更新为{ $set: { profile: ... } }
它将替换整个profile
文件。
您应该这样使用它:
$set: {
'profile.<field_name>': '<field_value>',
...
}
这是针对您的情况的代码:
const $set = {};
_.each(data, (value, key) => {
$set[`profile.${key}`] = value;
});
Meteor.users.update(this.userId, { $set });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.