lundi 2 mars 2015

MongoDB 2.0.18 unable to connect via node whereas older version 1.4.32 works

i installed mongodb via npm using



npm install mongodb


Which installed 1.4.32 version of mongodb driver. The code given at the bottom worked fine, but when i upgraded my driver ( and deleted the older folder of 1.4.32 from node modules, updated package.json) i'm getting the following error:


I guess its something to do with replicaSet option in url, i dont know how to set it, i mean i tried



mongodb://localhost/test?replicaSet=rs0-1


but is that all we need to configure a replica set, how can we use null for replica set..


here's the connection code i used in nodeJS that works fine with 1.4.* of mongodb native driver but doesn't work with 2.0.18 of mongodb native driver.



var express = require('../Express2/node_modules/express');
var app = express(); // instantiate the express object thru its constructor
var path = require('path');
var assert = require('assert');
var mongodb = require('mongodb'); // 2.0.18

// use /public directory for serving web pages first
app.use(express.static(path.join(__dirname , '/public'))); // add current directory

// when a request for /insert is received
app.get("/insert",function(req,resp){
// Create a mongo client
var mongoClient = mongodb.MongoClient;

// configure url to connect to
var url = 'mongodb://localhost:27017/test';
//http://mongodbuser:pass@ipaddr:port/dbname

// try to connect and get db handle
mongoClient.connect(url,function(err,db){
if(err){
console.log(err.toString()); // Error thrown here..
}
else{
console.log('successfully connected to mongod server');
var collectionStudent = db.collection('student');
// accepts obj and callback.
collectionStudent.insert(
{name:'Andrew',courses:[{subject:'RDBMS',fee:15000}]},
function(err,result){
resp.send('Result of insertion:' + JSON.stringify(result));
resp.end();
db.close();
}
);
}
});

});

// for pages not found in public directory, do this:
app.get("*",function(req,resp){
resp.sendFile(path.join(__dirname , "/public/404.html"));
});

// any other special routes like /result etc appear below this line
//..
//..
app.listen(3000);

Aucun commentaire:

Enregistrer un commentaire