0

**MongoServerSelectionError: connect ECONNREFUSED ::1:27 I am trying to connect to Mongodb, MongoDB server is started using mongod but still getting error.

I am using version 6.0 of mongodb here is my code`

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

const url = "mongodb://localhost:27017";

const client = new MongoClient(url);

async function run() {
  try {
    const database = client.db('sample_mflix');
    const movies = database.collection('movies');

   
    const query = { title: 'Back to the Future' };
    const movie = await movies.findOne(query);

    console.log(movie);
  } finally {
   
    await client.close();
  }
}
run().catch(console.dir);`

the error i am getting is :screenshot :mongoServerSelectionError

C:\Users\Alok Singh\OneDrive\Desktop\FruitsProject>node app.js
MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (C:\Users\Alok Singh\OneDrive\Desktop\FruitsProject\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] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: null,
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
Alok Singh
  • 17
  • 3
  • 1
    Either configure MongoDB to bind on IPv6 addresses or use `127.0.0.1` instead of `localhost` – Phil Jul 06 '23 at 01:33

0 Answers0