如何在不同的数据库中创建Meteor集合?

时间:2015-06-04 09:01:02

标签: mongodb meteor minimongo

我可以使用Test = new Meteor.Collection("testCollection")

创建新的流星集合

但它会在我的mongo安装的testCollection数据库中创建admin

假设我在mongo中有两个独立的数据库,如testing,另一个是admin。如何在mongo安装中的testing db中创建上面的集合?

此外,我可以指定某个地方,我想限制/取消隐藏特定的集合,以便定义集合的大小。

1 个答案:

答案 0 :(得分:3)

如果您只想使用testing数据库,则可以在调用应用程序之前覆盖MONGO_URL环境变量(例如,使用正确的数据库URL):

$ export MONGO_URL=mongodb://localhost:27017/testing
$ meteor

如果您想在应用中使用不同的数据库,则应使用the new _driver parameter。只需使用相同的mongo url作为默认数据库,但替换数据库名称!

  // this replace is just for explicit demonstration. Static string is advised
  var mongo_url = process.env.MONGO_URL.replace("/admin","/testing");
  var testing = new MongoInternals.RemoteCollectionDriver(mongo_url);
  Test = new Mongo.Collection("testCollection", { _driver: testing });

对于上限集合,已在this meteor issue中正确回答,并由this commit修正:

col1 = new Meteor.Collection("myCollection");
coll._createCappedCollection(numBytes, maxDocuments);

据我所知,你无法取消以前加盖的收藏品。

请注意,要使这些方法起作用,您必须在服务器和客户端之间分离收集创建,因为客户端无法访问您的服务器的数据库。在客户端中,只需像往常一样创建集合,其名称与服务器版本相同:

if (Meteor.isServer) {
  var testing = new MongoInternals.RemoteCollectionDriver("<mongo url testing>");
  Test = new Mongo.Collection("testCollection", { _driver: testing });
  Test._createCappedCollection(2000000, 500); // capped to 2,000,000 Bytes, 500 documents
}
else {
  Test = new Meteor.Collection("testCollection");
}
相关问题