问题描述
目前正在学习nodejs+mongodb
环境基本搭建完成。
根据文档,访问http://localhost:27017/能够显示You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number表明 mongodb服务已经启动。
执行连接数据库的代码 无法连接到数据库
希望有大佬能够指点一二,不胜感激!!!
执行node app.js代码后控制台的打印
connection error: { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoError: Server at localhost:27017 reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)]
相关代码
app.js:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test',{ useNewUrlParser: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('we\'re connected!')
});
控制台打印信息中能看到 上述代码执行时有尝试连接数据库,但是连接上后就被断开。
Sat Aug 10 21:56:45.997 [initandlisten] connection accepted from 127.0.0.1:59972 #4 (1 connection now open)
Sat Aug 10 21:56:46.024 [conn4] end connection 127.0.0.1:59972 (0 connections now open)
这个错误信息说的很清楚了,服务器与客户端的 wire 版本不兼容。
mongoosejs 有一个跟 mongodb 的兼容性表格:
查一查是不是对得上。