I set up a MongoDB database to use with a simple node.js project I created many months ago and it was working fine back then. I can still connect to it and query it from the command line just fine (using the mongo
CLI) but cannot seem to connect to it from within node. When I use this code to connect:
import MongoClient from 'mongodb';
const mongoUrl = 'mongodb://localhost/my-project?replicaSet=rs0';
MongoClient.connect(mongoUrl, { useUnifiedTopology: true }).then(() => {
console.log('success');
}).catch(e => {
console.error(e);
process.exit(1);
});
I encounter this error message:
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
at Timeout._onTimeout (/srv/http/bugs/private/node_modules/mongodb/lib/core/sdam/topology.js:438:30)
at listOnTimeout (node:internal/timers:568:17)
at processTimers (node:internal/timers:510:7) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map(1) { 'localhost:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null
}
}
I found this similar question when searching for solutions, but I don’t think their fix makes sense for me. I have a very simple setup with mongo and node running on the same machine. I don’t entirely remember why I set up replication, but it was only because of some sort of warning I had encountered back when I set this up. The contents of the database are not important, so I don’t mind if the data gets destroyed, I’d just like to be able to connect to mongo from node again. Thanks in advance for any help!