我正在尝试使用node.js在我的Windows系统上与mongodb连接。但是,它给我一个错误:
C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\lib\mongo_client.js:225
throw err
^
MongoError: failed to connect to server [127.0.0.1:27017] on first connect
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
at null.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:260:12)
at g (events.js:260:16)
at emitTwo (events.js:87:13)
at emit (events.js:172:7)
at Socket.<anonymous> (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:162:49)
at Socket.g (events.js:260:16)
at emitOne (events.js:77:13)
这是我的代码:
"use strict";
const MongoClient = require('mongodb').MongoClient;
const dbUrl = 'mongodb://127.0.0.1:27017/cs602';
MongoClient.connect(dbUrl, (err, db) => {
if (err)
{throw err}else {
console.log('Successfully connected to',
db.s.databaseName);
}
// Do database operations
db.close();
});
我是node.js和mongodb的新手,有没有人可以帮我这个?非常感谢你!
答案 0 :(得分:2)
此错误可能是由MongoDB的绑定IP设置引起的。您可以查看MongoDB的配置文件。但事实是我不知道是否位于Windows系统中。
在我的例子中,绑定IP设置为localhost地址,如下所示:
preg_replace