MongoDB 中的 Meteor 重命名 Meteor.users 集合名称

Meteor renaming Meteor.users collection name in MongoDB

我们有多个网站指向同一个 MongoDB。例如前置 public 网站、内部管理网站等

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

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

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

不,遗憾的是,这是硬编码到包中的,正如 Brian 所说,该包没有自定义空间。

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

例如用户文档:

{
  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();
  }
});

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

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

Meteor.users = Accounts.users;