2

I'm facing an issue while trying to connect to MongoDB from my Node.js application using the mongodb driver.

I have a MongoDB server running on my local machine, and I can successfully connect to it using MongoDB Compass and the VS Code extension.

However, when I attempt to connect from my Node.js application, I encounter an error.

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

const uri = 'mongodb://localhost:27017/';
const client = new MongoClient(uri);

client.connect((err) => {
  if (err) {
    console.error(err);
    return;
  }
  console.log('Connected to MongoDB');

  // ... continue with my code
});

The error message I receive is:

MongoServerSelectionError: connect ECONNREFUSED ::1:27017
  // rest of the error details...

I was try to connect to MongoDB from a Node.js app.

I have already checked the following:

  • The MongoDB server is running on my local machine, and I can connect using MongoDB Compass and VS Code extension.
  • I have installed the mongodb package using npm, and I tried different versions of the package as well.
  • There is no authentication required to connect to my MongoDB server.
  • I have also tried specifying the version of the mongodb package during installation, but the issue persists.

Why I can't connect to MongoDB from my Node.js application? Am I missing something in my code or configuration?

ghybs
  • 47,565
  • 6
  • 74
  • 99

2 Answers2

1

Try replacing localhost in the URI by explicit locsl IPv4 127.0.0.1:

const uri = 'mongodb://127.0.0.1:27017/';

As explained on mongodb driver for Node:

NOTE: Resolving DNS Connection issues

Node.js 18 changed the default DNS resolution ordering from always prioritizing ipv4 to the ordering returned by the DNS provider. In some environments, this can result in localhost resolving to an ipv6 address instead of ipv4 and a consequent failure to connect to the server.

This can be resolved by: [...]

  • using a host of 127.0.0.1 in place of localhost
ghybs
  • 47,565
  • 6
  • 74
  • 99
0

hi everyone thank you all for your helpful comments, the answer to this problem is in two steps

  1. the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1, However, net.ipv6 defaults to false. the solution to this is to either

A. set them in mongod.conf file

  ipv6: true
  bindIpAll: true

or

  ipv6: true
  bindIp: localhost

B. just add them to the command when starting Mongo server

mongod --bind_ip_all --ipv6

  1. I didn't have a mongod.conf file soo I had to write one, here is what it should look like
# mongod.conf

# Where to store data.
storage:
  dbPath: C:\data\db

# IPv6 support.
net:
  ipv6: true
  bindIpAll: true

# Port to listen on (default is 27017).
# port: 27017

# Enable/disable journaling.
# storage:
#   journal:
#     enabled: true

# Set log files.
# systemLog:
#   destination: file
#   path: C:\data\log\mongod.log

# Replica Set Options.
# replication:
#   replSetName: "rs0"

# Authentication Options.
# security:
#   authorization: enabled

# More options and configurations can be added as needed.

the first two and are enabled the others are optional

also I would refer to this problem as it's like mine Can't connect to MongoDB 6.0 Server locally using Nodejs driver