0

I am trying to connect to my local mongodb and write to it just for testing. i can't seem to connect to it when using node.js, but when i try to connect and interact with it in python everything works fine.

I am running mongodb v6.0

I can see it running when i run mongod in cmd

I tried turning off my fire wall, checking if it's running, etc. even chatgpt is lost

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017';

MongoClient.connect(url, function(err, client) {
  if (err) throw err;

  const db = client.db('inventory');
  db.createCollection('klci', function(err, res) {
    if (err) throw err;
    console.log('Collection klci created!');
    client.close();
  });
});

this is the error message i am getting

C:\Users\Laith\Desktop\mongodb test\node_modules\mongodb\lib\sdam\topology.js:278
                const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
                                     ^

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (C:\Users\Laith\Desktop\mongodb test\node_modules\mongodb\lib\sdam\topology.js:278:38)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 1105815,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\Users\Laith\Desktop\mongodb test\node_modules\mongodb\lib\cmap\connect.js:370:20)
            at Socket.<anonymous> (C:\Users\Laith\Desktop\mongodb test\node_modules\mongodb\lib\cmap\connect.js:293:22)
            at Object.onceWrapper (node:events:628:26)
            at Socket.emit (node:events:513:28)
            at emitErrorNT (node:internal/streams/destroy:151:8)
            at emitErrorCloseNT (node:internal/streams/destroy:116:3)
            at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
          cause: Error: connect ECONNREFUSED ::1:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
            errno: -4078,
            code: 'ECONNREFUSED',
            syscall: 'connect',
            address: '::1',
            port: 27017
          },
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

Node.js v18.16.0

This is my net settings in my config file

net: ipv6: true bindIp: localhost port: 27017

mongodb log file when i try to connect

{"t":{"$date":"2023-05-13T08:40:02.765-06:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"[::1]:52502","uuid":"4dc4df45-e01f-4eed-b17f-8ce5f3abe849","connectionId":5,"connectionCount":4}} {"t":{"$date":"2023-05-13T08:40:02.766-06:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn5","msg":"client metadata","attr":{"remote":"[::1]:52502","client":"conn5","doc":{"driver":{"name":"nodejs","version":"5.5.0"},"platform":"Node.js v18.16.0, LE","os":{"name":"win32","architecture":"x64","version":"10.0.19045","type":"Windows_NT"}}}}

  • Does this answer your question? [MongoError: connect ECONNREFUSED 127.0.0.1:27017](https://stackoverflow.com/questions/46523321/mongoerror-connect-econnrefused-127-0-0-127017) – Yeshwin Verma May 13 '23 at 07:34
  • 1
    Does this answer your question? [Can't connect to MongoDB 6.0 Server locally using Nodejs driver](https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver) – Wernfried Domscheit May 13 '23 at 08:32
  • @YeshwinVerma Error message `MongoError: connect ECONNREFUSED ::1:27017` is different to `MongoError: connect ECONNREFUSED 127.0.0.1:27017` and mongod is running (because it works with other clients). Thus the answer you linked does not provide the solution. – Wernfried Domscheit May 13 '23 at 08:37
  • @wernfriendomscheit thanks for the suggestion. i changed my net settings in my cfg and now it's still not able to connect. – Laith Haddadin May 13 '23 at 14:27

0 Answers0