MongoDB 2.4 MongoClient.Connect失败

问题描述

我在RaspBerry Pi(运行最新的Raspbian)上使用MongoDB 2.4.14版。我有一个简单的测试文件试图使mongo正常工作:


let url = 'mongodb://172.20.0.239:27017/'
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url,{ useUnifiedTopology: true },function(err,db) {
    if (err) throw err;
    var dbo = db.db(<database>);

    var cursor = dbo.collection("Players").find();
    if (cursor == null) {
        console.log('cursor nulo');
    } else {
        console.log('cursor n nulo');
        var strArray = [];
        cursor.each(function (err,item) {
            if(item!=null){
                strArray.push(item.datetime+' '+item.onlineUsers);
                console.log(item.datetime+' '+item.onlineUsers);
            }
            else{
                dbo.close();
            }
        });
    }
});

我遇到了错误

MongoServerSelectionError: Server at 172.20.0.239:27017 reports maximum wire version 0,but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)

我的代码有问题吗?

解决方法

现代MongoDB驱动程序支持MongoDB服务器2.6及更高版本。您正在使用2.4服务器。

如果可能,我建议将服务器至少升级到2.6。如果无法做到这一点,则需要寻找支持2.4的旧版(此时真的很旧)驱动程序。