【问题标题】:Meteor renaming Meteor.users collection name in MongoDBMeteor 在 MongoDB 中重命名 Meteor.users 集合名称
【发布时间】:2015-12-02 07:19:49
【问题描述】:

我们有不止一个网站指向同一个 MongoDB。例如前端公共网站、内部管理网站等。

我们希望为不同的网站收集不同的用户。在使用 Meteor.users 变量访问用户集合时,有什么方法可以指示 Meteor 在实际数据库中使用不同的集合名称。

【问题讨论】:

  • 最好用“应用”这个词而不是“网站”!谢谢

标签: meteor meteor-accounts


【解决方案1】:

从查看源代码可以看出,集合名称是硬编码在accounts-base 包中的。我没有看到任何通过代码设置名称的选项。

Meteor.users = new Mongo.Collection("users", {
      _preventAutopublish: true,
      connection: Meteor.isClient ?     Accounts.connection : Meteor.connection
});

【讨论】:

    【解决方案2】:

    不,遗憾的是,这是硬编码到包中的,正如布赖恩所说,包没有定制空间。

    但是,您可以非常轻松地为 Meteor.users 集合中的每个文档添加一个新键 accountTypeaccountType 可以指定该用户属于前端公共网站还是内部管理网站。

    例如用户文档:

    {
      username: "Pavan"
      accountType: "administrator"
      // other fields below
    }
    

    当然,您可以从那里发布特定数据,或根据accountType 的值启用网站的不同部分。

    例如,如果我希望管理员能够订阅,并查看所有用户信息:

    Meteor.publish("userData", function() {
      if (this.userId) {
        if (Meteor.users.find(this.userId).accountType === "admin") {
          return Meteor.users.find();
        } else {
          return Meteor.users.find(this.userId);
        }
      } else {
        this.ready();
      }
    });
    

    【讨论】:

      【解决方案3】:

      这没有经过测试,但乍一看,这可能是更改用户集合名称的可行方法。将此代码放在 /lib 文件夹中的某个位置:

      Accounts.users = new Mongo.Collection("another_users_collection", {
        _preventAutopublish: true,
      });
      
      Meteor.users = Accounts.users;
      

      【讨论】:

      • 我测试了它,令人惊讶的是它几乎没有问题。唯一在我的原型上不起作用的是 Meteor.user() 所以我将“new Mongo.Coll ...”分配给一个变量,然后将该变量分配给 Accounts.users,并将其导出以使用它我需要手动调用此集合的其他地方。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2012-02-02
      • 2015-04-01
      • 1970-01-01
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多