0

を使用して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を使用するにはどうすればよいですか..

これは、1.4.* の mongodb ネイティブ ドライバーでは正常に動作しますが、2.0.18 の mongodb ネイティブ ドライバーでは動作しない nodeJS で使用した接続コードです。

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);
4

0 に答える 0