2017-01-28 33 views
0

您如何使用Mongoose:https://github.com/Automattic/mongoose節點,Mongoose連接到IBM Bluemix Compose for Mongodb

這裏的例子:https://github.com/IBM-Bluemix/compose-mongodb-helloworld-nodejs工作得很好。 下面是示例蒙戈代碼的簡化片斷:

MongoClient.connect(credentials.uri, { // step 1: connect 
    mongos: {...}, 
    function(err, db) { 
     if (err) { 
      console.log(err); 
     } else { 
      mongodb = db.db("examples"); // step 2: create or use database 
     } 
    } 
); 

我不能找到使用兩步連接處理一個例子貓鼬。

我注意到,MongoDB的Compose不支持直接連接到現有的示例數據庫。 連接到這個網址:

mongodb://admin:[email protected]:22601,bluemix...0.dblayer.com:22601/examples' 

會導致「MongoError:驗證失敗」

回答

0

這裏是從使用撰寫MongoDB的和貓鼬的樣品的摘錄:

var mongoDbUrl, mongoDbOptions = {}; 
var mongoDbCredentials = appEnv.getServiceCreds("mycomposedb").credentials; 
var ca = [new Buffer(mongoDbCredentials.ca_certificate_base64, 'base64')]; 
mongoDbUrl = mongoDbCredentials.uri; 
mongoDbOptions = { 
    mongos: { 
    ssl: true, 
    sslValidate: true, 
    sslCA: ca, 
    poolSize: 1, 
    reconnectTries: 1 
    } 
}; 
console.log("Connecting to", mongoDbUrl); 
mongoose.connect(mongoDbUrl, mongoDbOptions); // connect to our database 

然後你可以用useDb切換數據庫。

The full source code is here

0

無論是弗雷德裏克的代碼片段(使用Mongo的驅動程序),也沒有他聯繫的源代碼(連接到管理DB)就會使用貓鼬連接到一個自定義的MongoDB。

爲了與貓鼬 IBM撰寫連接到一個自定義的數據庫,你必須提供比撰寫提供的默認不同的連接字符串。

下面的連接字符串模板工程:

var connectionUrl = 'mongodb://<username>:<password>@<hostname>:<port>,<hostname-n>:<port-n>/<db-name>?ssl=true&authSource=admin'; 

使用下列選項:

var sslCA = [fs.readFileSync('mongo.cert')]; 
var options = { 
    ssl: true, 
    sslValidate: true, 
    sslCA, 
}; 

我對Github

提供 the complete working example
相關問題