Title: Unable to establish connection with MongoDB server: MongooseServerSelectionError
Description: When running the following code to connect to a MongoDB server using Mongoose:
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/mydb").then(() => {
console.log("Connection Successful");
}).catch((error) => {
console.log("Something went wrong!", error);
});
I encounter the following error:
"Something went wrong! MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at _handleConnectionErrors (d:\DB Server\node_modules\mongoose\lib\connection.js:792:11)
at NativeConnection.openUri (d:\DB Server\node_modules\mongoose\lib\connection.js:767:11) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: null,
maxElectionId: null,
maxSetVersion: null,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}"
Issue: I am unable to establish a connection with the MongoDB server. The code attempts to connect to the MongoDB database specified in the connection string (mongodb://localhost:27017/mydb), but it throws the MongooseServerSelectionError with the message connect ECONNREFUSED ::1:27017.
What did you try and what were you expecting? I tried to establish a connection to the MongoDB server using the Mongoose library. I expected the connection to be successful, indicated by the message "Connection Successful" being logged to the console. What actually resulted? However, instead of a successful connection, I received an error. The error message displayed "Something went wrong!" followed by the error details, specifically the MongooseServerSelectionError with the message connect ECONNREFUSED ::1:27017.
Additional Information:
Node.js version: v18.16.0 MongoDB version: 6.0 Operating system: Windows 11