2015-04-05 54 views
2

我们有多个网站指向同一个MongoDB。例如面向公众网站,内部管理网站等。流星在MongoDB中重命名Meteor.users集合名称

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

+0

如果使用“应用程序”一词而不是“网站”,则会更好!谢谢 – 2016-07-30 18:20:12

回答

1

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

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

不,可悲的是,这是硬编码到软件包中,正如Brian所说,软件包没有提供定制空间。

但是,您可以非常轻松地为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(); 
    } 
}); 
1

这不是测试,而是从第一次看,这可能是一个可行的方法更改用户集合的名称。将此代码放置在/ lib文件夹中的某处:

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

Meteor.users = Accounts.users; 
+1

我测试它,它出乎意料地工作几乎没有问题。唯一不能用于我的原型的是Meteor.user(),因此我将“new Mongo.Coll ...”分配给一个变量,然后将该变量分配给Accounts.users,并将其导出以便在其中使用它我需要手动调用这个集合的其他地方。非常感谢。 – 2016-07-30 18:24:55