I keep getting error when i try connecting node to mongodb

it seems there is an issuea with “6.9.0”, downgrading “6.1.0” (which I use my older projects) worked for me

This worked for me too!! thanks !

1 Like

Apparently, the issue is on version 6.9.0 which contains a breaking change:

Here are two ways I found to overcome this issue:

  1. downgrading to a lower version (6.8.2 for ex)
  2. passing autoSelectFamily: false overriding the default value of true

Hope someone on MongoDB will see this discussion and revert this breaking change, or alternatively - document this as a breaking change on their release notes.

P.S.
I would totally understand if this new behavior was communicated on previous release notes, allowing developers to get prepared for that (+ adding a runtime warning might do as well), but since I didn’t find any evidence for that - it is a breaking change published out on 6.9.0 without any prior notification.

4 Likes

upgrading to latest node version worked for me as well!

1 Like

Thanks alot man this is really helpoed me with option 2 adding the autoSelectFamily: false

Here a code that solve the issue for me:


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

const uri =
  "mongodb+srv://<name>:<pw>@<server>/<db></db>?retryWrites=true&w=majority";

const client = new MongoClient(uri, {
  tls: true,
  serverSelectionTimeoutMS: 3000,
  autoSelectFamily: false,
});

async function connectToDatabase() {
  try {
    await client.connect();
    console.log("Connected to MongoDB successfully.");
    // Add your database operations here
  } catch (error) {
    console.error("Connection failed", error);
  } finally {
    await client.close();
  }
}

connectToDatabase();

1 Like

Amazing, Option 2 was a life saver!

autoSelectFamily: false - This was what I needed. Thanks!

if you mongoose version is 8> the problen never disapire you need to use a old version like 6 or 7 that resolved the error

Than you so much, I’m new to Mongodb so this was very frustrating experience… Youre suggestion to use autoSelectFamily: false solved the problem!

i know im mssing the version but wont work still

Im trying to connect to my database from Render, when i do it locally it works but when i deploy my app on render it dosent i get the same error i tried lowering the version and autoSelectFamily false but nothing works

const client = new MongoClient(uri, {
serverApi: {

    autoSelectFamily: false
},

});

this is my client idk if here is the problem or what, im using atlas and put the 0.0.0.0 to allow all conections but nothing

this error comes me to and i just install the latest version and the error gone. there is the comment npm i mongodb@latest

I upgraded to node version 20 along with “mongodb”: “^6.12.0”,
It solved the issue.