Create MongoDB driver instances optimized for serverless environments.
Instances are cached between requests to minimize the number of connections and improve performance.
npm install mongodb-serverless
import { getMongoClient } from "mongodb-serverless";
export async function handler(event) {
const client = getMongoClient(process.env.MONGO_URI, { appName: "app" });
return await client.db().collection("docs").find({}).toArray();
}