首页 文章

Meteor确实看到了MongoInternals.RemoteCollectionDriver的远程mongodb实例

提问于
浏览
3

使用远程mongodb和新流星1.3时遇到问题

var d = new MongoInternals.RemoteCollectionDriver("<mongo url>");
C = new Mongo.Collection("<collection name>", { _driver: d });

我把它放在我的集合文件夹中

if(Meteor.isServer){
    var driver = new MongoInternals.RemoteCollectionDriver("mongodb://user:password@localhost:27017/customCollec");
} 
C = new Mongo.Collection("customCollec", { _driver: driver });

但是在客户端这样的调用会让我回复: C 没有定义

console.log("" + C.find().count());

所以我在我的collections.js中测试了这样的同一行:

if(Meteor.isServer){
    var driver = new MongoInternals.RemoteCollectionDriver("mongodb://user:password@localhost:27017/customCollec");
    C = new Mongo.Collection("customCollec", { _driver: driver });
    console.log("" + C.find().count());
}

但结果是一样的: C 未定义

另外我的设置是自动发布和不安全(dev staging)

提前感谢任何线索 .

1 回答

  • 2

    好吧,我终于想通了(流星1.3,自动发布)!

    在lib / collections.js中

    var database;
    if(Meteor.isServer){
        console.log("On collections ");
        database = new MongoInternals.RemoteCollectionDriver("mongodb://user:password@0.0.0.0:27017/db_name");
    }
    
    MyRemoteCollection = new Mongo.Collection('db_name', { _driver: database });
    

    在此之后,我能够在客户端获得 Value

    console.log("MyRemoteCollection count = " + MyRemoteCollection.find().count());
    

    当然它只在加载集合时才有效 .

    “希望它会有所帮助;)

相关问题