MongoError:驱动程序与此服务器版本不兼容?

时间:2015-10-30 08:01:28

标签: javascript node.js mongodb express

这是对我的程序的依赖:

var express = require('express'),
    app = express(),
    cons = require('consolidate'),
    MongoClient = require('mongodb').MongoClient,
    mongodb = require('mongodb'),
    Server = require('mongodb').Server;


var mongoclient = new MongoClient(new Server("localhost", 27017));
var db = mongoclient.db('prisync_mamy');

此查询在使用mongo 2.6

的nodejs中正常工作
 db.collection('coll').insert_one(documen , function(err , records){
                if (err) throw err;
            });

但是当升级mongo 2.6到3.x时,它会出错:

  

MongoError:驱动程序与此服务器版本不兼容   Object.toError   (/home/ekodev/Documents/ComParice/node_modules/mongodb/lib/mongodb/utils.js:110:11)   在__executeInsertCommand   (/home/ekodev/Documents/ComParice/node_modules/mongodb/lib/mongodb/db.js:1829:27)   在Db._executeInsertCommand   (/home/ekodev/Documents/ComParice/node_modules/mongodb/lib/mongodb/db.js:1930:5)   在insertAll   (/home/ekodev/Documents/ComParice/node_modules/mongodb/lib/mongodb/collection/core.js:205:13)   在Collection.insert   (/home/ekodev/Documents/ComParice/node_modules/mongodb/lib/mongodb/collection/core.js:35:3)   在app.get.pro_url(/home/ekodev/Documents/ComParice/app.js:72:47)
  在回调   (/home/ekodev/Documents/ComParice/node_modules/express/lib/router/index.js:164:37)   在参议院   (/home/ekodev/Documents/ComParice/node_modules/express/lib/router/index.js:138:11)   在通过   (/home/ekodev/Documents/ComParice/node_modules/express/lib/router/index.js:145:5)   在Router._dispatch   (/home/ekodev/Documents/ComParice/node_modules/express/lib/router/index.js:173:5)

2 个答案:

答案 0 :(得分:1)

打开cmd并输入以下命令

 npm install mongodb@1.4.x

然后使用以下代码运行替换代码

    var express = require('express'),
        app = express(),
        MongoClient = require('mongodb').MongoClient,
        mongodb = require('mongodb'),
        Server = require('mongodb').Server;

    var MongoClient = require('mongodb').MongoClient
      , Server = require('mongodb').Server;

    var mongoClient = new MongoClient(new Server('localhost', 27017));
    mongoClient.open(function(err, mongoClient) {
      var db1 = mongoClient.db("mydb");
        console.log('it\'s working ');
      mongoClient.close();
    });

答案 1 :(得分:1)

在我的情况下npm install mongoose@3.0.x工作,或者使用您的特定mongoDB驱动程序版本尝试npm install mongoose