MongoDB Native Drive Example (NodeJS / Export & Import / Await & Async) db.js const mongodb = require('mongodb'); const url = 'CONNECTION_URL'; const options = {}; const database = 'DATABASE_NAME'; const client = new mongodb.MongoClient(url, options); module.exports = { connect: async () => (await client.connect()).db(database), disconnect: () => client.close(), ...mongodb, }; index.js const { connect, disconnect, ObjectId } = require('./db'); async function sampleFunction() { const db = await connect(); console.log(await db.collection('posts').find().toArray()); disconnect(); } sampleFunction();
Hope this helps someone.