MongoDB 2.0.18无法通过节点连接,而旧版本1.4.32可以工作



我使用通过npm安装mongodb

npm install mongodb 

安装了1.4.32版本的mongodb驱动程序。底部给出的代码运行良好,但当我升级驱动程序时(并从节点模块中删除了1.4.32的旧文件夹,更新了package.json)我得到以下错误:

unknown system error : 10042. 

我在搜索引擎上查了一下,发现了一个windows winsock错误,不知道为什么我在最新版本中得到了这个,而在早期版本中没有。。

我想这与url中的replicaSet选项有关,我不知道如何设置,我的意思是我尝试过

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

但是,我们只需要配置一个副本集,我们如何使用null作为副本集。。

以下是我在nodeJS中使用的连接代码,它可以很好地与mongodb本机驱动程序的1.4.*配合使用,但不能与mongodb本机驱动软件的2.0.18配合使用。

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';
  //mongodb://user: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);

尝试使用

 var url = 'mongodb://127.0.0.1:27017/test';

 var url = 'mongodb://0.0.0.0:27017/test';

此外,检查0.0.0.0的含义

而不是

localhost

  var url = 'mongodb://localhost:27017/test';

最新更新