Unable to connect to my mongo DB after node version upgrade

I recently updated my node version to the most recent stable one - v20.11.1.
Also, I updated my mongodb node driver version on the package.json to ^6.5.0.

Then I tried connecting to my DB using exactly the same code snippet on the mongo docs only using my URI:

const { MongoClient, ServerApiVersion } = require("mongodb");
// Replace the placeholder with your Atlas connection string
const uri = "<my connection string>";
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri,  {
        serverApi: {
            version: ServerApiVersion.v1,
            strict: true,
            deprecationErrors: true,
        }
    }
);
async function run() {
  try {
    // Connect the client to the server (optional starting in v4.7)
    await client.connect();
    // Send a ping to confirm a successful connection
    await client.db("admin").command({ ping: 1 });
    console.log("Pinged your deployment. You successfully connected to MongoDB!");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

The connection did not succeed and it I got the following error:
MongoParseError: option connect is not supported

The full error:

/Users/user/Documents/mongoConnect/node_modules/mongodb/lib/connection_string.js:273
        throw new error_1.MongoParseError(`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`);
        ^

MongoParseError: option connect is not supported
    at parseOptions (/Users/user/Documents/mongoConnect/node_modules/mongodb/lib/connection_string.js:273:15)
    at new MongoClient (/Users/user/Documents/mongoConnect/node_modules/mongodb/lib/mongo_client.js:52:63)
    at Object.<anonymous> (/Users/user/Documents/mongoConnect/mong.js:7:16)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  [Symbol(errorLabels)]: Set(0) {}
}

I searched for this kind of error but couldn’t find any mention of it in regards to option connect, only mostly in regards to useNewUrlParser and useUnifiedTopology.

Does anyone know why this happens and how do I fix it?
Thanks!

@Roee_Hasson which node version were you using before?
Because its an optional after version v4.7, So maybe try it without .connect()

Unfortunately that doesn’t work.
I tried now simply instantiating a MongoClient instance:

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

// Replace the placeholder with your Atlas connection string
const uri = "<my connection string>";

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri,  {
        serverApi: {
            version: ServerApiVersion.v1,
            strict: true,
            deprecationErrors: true,
        }
    }
);

And got the same result:

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

// Replace the placeholder with your Atlas connection string
const uri = "mongodb+srv://dev-user:3kv2dPQGSG5AyFty@development.oh4jb.mongodb.net/test?connect=replicaSet";

// Create a MongoClient with a MongoClientOptions object to set the Stable API version
const client = new MongoClient(uri,  {
        serverApi: {
            version: ServerApiVersion.v1,
            strict: true,
            deprecationErrors: true,
        }
    }
);

@Aman_Saxena I was using node version v14.21.3.

@Roee_Hasson I believe you are using replicasets with multiple nodes. Correct me if I am wrong.
In Replicaset you need to give all replicaset in URI
Refer to this article once https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/connect/#connect-to-a-replica-set

connect=replicaSet remove this from the connection string

I cannot recall having ever seen this option.

Wow I would have never thought that it had something to do with the replicaSet, but it did!
I changed it to the way the article suggests and now it works great!

Thanks so much!

1 Like

mongodb+srv://user:password@development.oh4jb.mongodb.net/test

This will work. The error is in the invalid option connect using the mongodb+srv is the recommended connection method.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.