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

时间:2015-04-05 02:46:01

标签: meteor meteor-accounts

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

我们希望为不同的网站提供不同的用户集合。有没有办法指示Meteor在使用Meteor.users变量访问用户集合时在实际DB中使用不同的集合名称。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

不,遗憾的是,这是硬编码到包装中,正如Brian所说,包装没有提供定制的空间。

但是,您可以非常轻松地为accountType集合中的每个文档添加新密钥Meteor.usersaccountType可以指定该用户是属于面向前方的公共网站还是属于内部管理网站。

例如,用户文档:

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

答案 2 :(得分:1)

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

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

Meteor.users = Accounts.users;
相关问题