error while connecting to mongo atlas using aws lambda

hi,

I’m trying connect to mongo atlas using aws lambda and using layers. I’ve been trying to connect to mongo atlas and the cluster is open for all. i’m facing this error. i’m using mongodb package 6.8.0

Error connecting to MongoDB: TypeError: Cannot read properties of undefined (reading ‘connect’)

const logger = require(“./logger”);
const { MongoClient, ObjectId } = require(“mongodb”);

let db = null;
let mongouri;
let client;
const connectToDatabase = async () => {
if (db) {
return db;
}

mongouri = process.env.MONGO_URI;
console.log(“mongo uri:”,mongouri);
if (!client) {
try {
client = new MongoClient(mongouri, { useNewUrlParser: true, useUnifiedTopology: true });
} catch (error) {
console.log(“Error with mongo client:”, error.toString());
}
}

try {
await client.connect();
const database = client.db(“cs_db_dev”);
console.log(“Connected to MongoDB”);
} catch (error) {
console.log(“Error connecting to MongoDB:”, error.toString());
}

// logger.debug(“mongo client output check”, { client });
// logger.debug(“Connected to mongodb database”);
// db = client.db(‘cs_db_dev’);
// logger.debug(“mongo client db”, { db });
return database;
};

const insertMany = async (db, collectionName, data) => {
return db.collection(collectionName).insertMany(data);
};

const upsert = async (db, collectionName, filter, update) => {
return db
.collection(collectionName)
.updateOne(filter, update, { upsert: true });
};

const find = async (db, collectionName, filter) => {
return db.collection(collectionName).find(filter).toArray();
};

const deleteMany = async (db, collectionName, filter) => {
return db.collection(collectionName).deleteMany(filter);
};

const bulkWrite = async (db, collectionName, bulkOps) => {
return db.collection(collectionName).bulkWrite(bulkOps);
};

const getCollection = (db, collectionName) => db.collection(collectionName);

module.exports = {
connectToDatabase,
insertMany,
upsert,
find,
deleteMany,
bulkWrite,
getCollection,
ObjectId,
};