skip to Main Content

I am trying to work with MongoDB in Node.js but it is not working with the simplest code.

The Simplest Code:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function (err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});

error:

D:ClgSem-5Software PackagesLabPractical-7node_modulesmongodblibsdamtopology.js:292
                const timeoutError = new error_1.MongoServerSelectionError(`Server selection timed out after ${serverSelectionTimeoutMS} ms`, this.description);
                                     ^

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (D:ClgSem-5Software PackagesLabPractical-7node_modulesmongodblibsdamtopology.js:292:38)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507: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: 2642697,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (D:ClgSem-5Software PackagesLabPractical-7node_modulesmongodblibcmapconnect.js:387:20)
            at Socket.<anonymous> (D:ClgSem-5Software PackagesLabPractical-7node_modulesmongodblibcmapconnect.js:310: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:1247: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.7.0

I tested error in both latest version as well as v5.0.13 but I have same error & when I searched on internet I found there should be problem with MongoDB service but when I checked for same. I found that service is already running.

2

Answers


  1. Chosen as BEST ANSWER

    Changes to the file in the given path will be helpful

    "C:Program FilesMongoDBServer5.0binmongod.cfg"
    

    at least these 4 lines must be inside the "net" portion

    net:
      port: 27017
      bindIp: 127.0.0.1
      ipv6: true
      bindIpAll: true
    

  2. ECONNREFUSED usually means MongoDB is not running at the URL you are supplying, or that your application cannot access that port. Are you definitely running MongoDB locally on port number 27017? You will need to make sure that your local MongoDB instance is up and available – try accessing it through the browser by navigating to localhost:27017.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search