我有一件大事 - 我过去几周开发的 meteor 应用程序终于上线了。但是,为了进行更新,我需要向我的用户个人资料添加一个字段。
我认为使用以下代码来隔离方法会起作用:
updateUsrs_ResetHelps: function(){
if(Meteor.users.update({}, {
$set: {
'profile.helps': []
}
}))
console.log("All users profile updated : helps reset");
else
throw new Meteor.Error(500, 'Error 500: updateUsrs_ResetHelps',
'the update couldn\'t be performed');
}
问题是我的用户拥有经典的 Meteor.accounts 文档,其中包含电子邮件、_id、服务、个人资料等...但是,在个人资料中,他们没有 .helps 字段。我需要创建它。
对于 future 的用户,我修改了帐户创建功能以在他们注册时添加此字段,但对于我已经注册的 200 个用户,我确实需要一个解决方案。
编辑:可能是因为更新中的选择器?简单的 {} 选择器是否可以一次更新集合中的所有用户/文档?
最佳答案
来自 Mongo 文档 (http://docs.mongodb.org/manual/reference/method/db.collection.update/):
By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.
如果您已经为新用户添加了字段,并且只需要修复旧的字段,为什么不直接在数据库中执行一次呢?
运行 meteor
启动您的应用程序,然后运行 meteor mongo
连接到数据库。然后对尚不存在该字段的记录运行更新。像这样的东西:
db.users.update({"profile.helps": {"$exists": false}}, {"$set": {"profile.helps": []}}, {multi:true})
Mongo文档将多参数指定为:
Optional. If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.
关于javascript - 如何向现有用户添加新字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30519496/