Biren Subudhi
Biren Subudhi6d ago

npm:mongodb connection issue, timeout

After facing bson corrupt errors frequently with https://deno.land/x/mongo@v0.33.0/mod.ts we started exploring options and replaced it with npm:mongodb@6.14.3 (latest) but facing connection issues with both Username - Password/X.509 auth options. Has anyone connected to mongodb recently with deno version 2, guide me on setting up the connection. we are facing timeout error with usernamee-password auth mechanism. X.509 has other issues.
1 Reply
Biren Subudhi
Biren SubudhiOP5d ago
It's resolved, mongodb documentation for nodejs was useful. Since importing MongoClient from deno package (https://deno.land/x/mongo@v0.33.0/mod.ts) was giving frequent BSONError: corrupt object bson so migrated to npm:mongodb
import { MongoClient, ServerApiVersion } from "npm:mongodb@6.14.2";
// import "https://deno.land/std@0.224.0/dotenv/load.ts"; // Load .env variables

const MONGO_USERNAME = Deno.env.get("MONGO_USERNAME");
const MONGO_PASSWORD = Deno.env.get("MONGO_PASSWORD");
const MONGO_APP_NAME = Deno.env.get("MONGO_APP_NAME");
const MONGO_CLUSTER_NAME = Deno.env.get("MONGO_CLUSTER_NAME");

const uri = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_CLUSTER_NAME}/?retryWrites=true&w=majority&appName=${MONGO_APP_NAME}`;


// 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,
},
});

const connectToMongoDB = async () => {
try {
console.log("Connecting to MongoDB...");
await client.connect();
console.log("Successfully connected to MongoDB");
return client.db("Faydo");
} catch (err) {
console.error("Error connecting to MongoDB:", err);
throw err;
}
};

const db = await connectToMongoDB();

export default db;
import { MongoClient, ServerApiVersion } from "npm:mongodb@6.14.2";
// import "https://deno.land/std@0.224.0/dotenv/load.ts"; // Load .env variables

const MONGO_USERNAME = Deno.env.get("MONGO_USERNAME");
const MONGO_PASSWORD = Deno.env.get("MONGO_PASSWORD");
const MONGO_APP_NAME = Deno.env.get("MONGO_APP_NAME");
const MONGO_CLUSTER_NAME = Deno.env.get("MONGO_CLUSTER_NAME");

const uri = `mongodb+srv://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_CLUSTER_NAME}/?retryWrites=true&w=majority&appName=${MONGO_APP_NAME}`;


// 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,
},
});

const connectToMongoDB = async () => {
try {
console.log("Connecting to MongoDB...");
await client.connect();
console.log("Successfully connected to MongoDB");
return client.db("Faydo");
} catch (err) {
console.error("Error connecting to MongoDB:", err);
throw err;
}
};

const db = await connectToMongoDB();

export default db;
The serverApi object passed to MongoClient is important to connect mongodb using nodejs/deno.

Did you find this page helpful?