NodeJS MongoDB驱动程序不兼容问题

时间:2015-10-23 13:45:01

标签: javascript json node.js mongodb

尝试使用MongoDB和NodeJS示例:

我正在尝试从名为Reddit的站点导入现有JSON: Mongo版本:

AMAC02PC0PHG3QP:dump macadmin $ mongo --version

MongoDB shell版本:3.0.6

Mongo驱动程序版本:     名称:mongodb     版本:1.3.23

猫鼬版     名字:猫鼬     版本:4.1.11

请求来自:http://www.reddit.com/r/technology/.json

我的 app.js 代码如下:

var MongoClient = require('mongodb').MongoClient;
var request = require('request');

MongoClient.connect('mongodb://localhost:27017/course', function(err, db){
    if(err) throw err;

    request('http://www.reddit.com/r/technology/.json', function(error, response, body){
        if (!error && response.statusCode == 200) {

            var obj = JSON.parse(body);

            var stories = obj.data.children.map(function(story){
                return story.data
            });

            // We got all our stories here.

            db.collection('reddit').insert(stories, function(err, data){
                console.dir("#############here");
                if(err) throw err;

                console.dir(data);

                db.close();
            });

        }
    });
});

当我跑步时:

node app.js

代码点击插入查询时出现此错误:

MongoError: driver is incompatible with this server version
    at Object.toError (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/lib/mongodb/utils.js:110:11)
    at __executeInsertCommand (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/lib/mongodb/db.js:1829:27)
    at Db._executeInsertCommand (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/lib/mongodb/db.js:1930:5)
    at insertAll (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/lib/mongodb/collection/core.js:205:13)
    at Collection.insert (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/lib/mongodb/collection/core.js:35:3)
    at Request._callback (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/app.js:18:37)
    at Request.self.callback (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/request/request.js:198:22)
    at emitTwo (events.js:87:13)
    at Request.emit (events.js:172:7)
    at Request.<anonymous> (/Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/request/request.js:1082:10)

1 个答案:

答案 0 :(得分:0)

我删除了mongodb模块并将其更新为最新版本一次。

AMAC02PC0PHG3QP:12_NodeJS_MongoDB_Importing_From_Other_Source macadmin$ npm install mongodb@latest
npm WARN package.json intro_to_npm@0.0.0 No repository field.
npm WARN package.json intro_to_npm@0.0.0 No README data
npm WARN package.json intro_to_npm@0.0.0 license should be a valid SPDX license expression
-
> kerberos@0.0.15 install /Users/macadmin/Desktop/NodeJS_MongoDB/12_NodeJS_MongoDB_Importing_From_Other_Source/node_modules/mongodb/node_modules/mongodb-core/node_modules/kerberos
> (node-gyp rebuild) || (exit 0)

  CXX(target) Release/obj.target/kerberos/lib/kerberos.o
  CXX(target) Release/obj.target/kerberos/lib/worker.o
  CC(target) Release/obj.target/kerberos/lib/kerberosgss.o
../lib/kerberosgss.c:509:13: warning: implicit declaration of function
      'gss_acquire_cred_impersonate_name' is invalid in C99
      [-Wimplicit-function-declaration]
        maj_stat = gss_acquire_cred_impersonate_name(&min_stat,
                   ^
1 warning generated.
  CC(target) Release/obj.target/kerberos/lib/base64.o
  CXX(target) Release/obj.target/kerberos/lib/kerberos_context.o
  SOLINK_MODULE(target) Release/kerberos.node
mongodb@2.0.46 node_modules/mongodb
├── es6-promise@2.1.1
├── readable-stream@1.0.31 (isarray@0.0.1, string_decoder@0.10.31, core-util-is@1.0.1, inherits@2.0.1)
└── mongodb-core@1.2.19 (bson@0.4.19, kerberos@0.0.15)