0

I have installed and set up mongodb compass v6 and am trying to connect to a test db using node.js. In my test folder I installed the mongodb drivers for node using npm install mongodb command and am running a node.js test script from there but keep getting server timeout error.

The node.js script I am using to test

var MongoClient = require('mongodb').MongoClient, format = require('util').format;
MongoClient.connect('mongodb://localhost:27017/local',
function (err, db) {
    if (err) {
        throw err;
    } else {
        console.log("successfully connected to the database");
    }
    db.close();
});

This is the error I get

>node test.js
..\mongodbTest\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:\xampp\htdocs\comp4621\mongodbTest\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: 10437142,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\xampp\htdocs\comp4621\mongodbTest\node_modules\mongodb\lib\cmap\connect.js:367:20)
            at Socket.<anonymous> (C:\xampp\htdocs\comp4621\mongodbTest\node_modules\mongodb\lib\cmap\connect.js:290: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.14.2

I have checked the mongodb uri and played with few different settings but no luck. Most of the soultions are outdated and can't seem to find something relevant to mongodb compass v6 with node,js v 18. My mongodb server is running on localhost:27017.

Any help would be great!

deep
  • 1
  • change localhost to 127.0.0.1 in the connection string – SemperFi Aug 03 '23 at 06:46
  • 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 Aug 03 '23 at 07:19

1 Answers1

0

Changing the localhost to 127.0.0.1 worked for me!

const { MongoClient } = require("mongodb");

//MongoDB deployment's connection string.
const uri = "mongodb://127.0.0.1:27017";

const client = new MongoClient(uri);
deep
  • 1