Connection Issues from nodejs


I am new to mongodb and have just started using it. I am going through a nodejs book and trying to connect to mongodb from my node js application. Both the application server and Db server are running on my windows local machine.

I am using the below code to connect to mongodb:

const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/my_database', {useNewUrlParser: true})

Also, the mongo server appears to be running as I see a MongoDB Database Server process in my task manager. I started this using mongod --config mongod.cfg. When I ran this command, there was nothing printed on console. Also, I am able to connect to DB using Compass.

Below is the error stacktrace. Any pointers on how to debug this issue:

const serverSelectionError = new ServerSelectionError();

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\connection.js:825:32) 
    at C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\index.js:411:10
    at C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\helpers\promiseOrCallback.js:41:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\helpers\promiseOrCallback.js:40:10)
    at Mongoose._promiseOrCallback (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\index.js:1285:10)
    at Mongoose.connect (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongoose\lib\index.js:410:20)        
    at Object.<anonymous> (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\index.js:43:10)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10) {
  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: 158728533,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongodb\lib\cmap\connect.js:387:20)
            at Socket.<anonymous> (C:\Users\riema\OneDrive\Desktop\Learning\nodejs-express-mongo-blog\node_modules\mongodb\lib\cmap\connect.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,
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  code: undefined

If your mongod is running as service there is no need to start mongod again from cmd line
Can you connect to your mongod from shell?
Try with instead localhost
May be the issue due to Ipv6 address
Search our forum threads for similiar issue

Thanks, using from nodesjs fixed it. But connection string with localhost worked fine when using Compass. Just wanted to understand what could be happening here?