如何向现有用户添加新字段

How to add new fields to existing users

我有一件大事 - 我过去几周一直在开发的流星应用程序终于上线了。但是,对于更新,我需要在我的用户个人资料中添加一个字段。

我认为用以下代码封闭一个方法会起作用:

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 字段。我需要创建它。

对于未来的用户,我修改了帐户创建功能以在他们注册时添加此字段,但对于我已经注册的 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.