DEV Community

Ali Hamza
Ali Hamza

Posted on

Day 54 of Learning MERN Srack

Hello Dev Community! 👋

It is officially Day 54 of my daily coding run toward full-stack MERN mastery! Yesterday, I configured relational pools with MySQL. Today, I made the official leap into the core NoSQL ecosystem by installing, connecting, and implementing MongoDB with my custom OOP Model Classes inside Prashant Sir's backend masterclass track!

Instead of maps, rows, and tables, my system now streams flexible BSON documents natively into a managed MongoDB collection engine!


🧠 Key Learnings From Node.js Lecture 28 (Native MongoDB Integration)

As shown in my active codebase files across "Screenshot (124).png" and "Screenshot (125).png", refactoring model structures into non-relational document channels gives your backend immense schema flexibility:

1. Persistent Pooling with MongoClient

Inside utils/databaseUtils.js, I imported mongodb and setup a dedicated connection driver pool. Instead of instantiating connections on every single API request, the server logs into Mongo_URL once, references the target database cluster context (_db), and exposes a clean runtime getter helper getdb():


javascript
let _db;

const mongoConnect = (callback) => {
    MongoClient.connect(Mongo_URL)
        .then((client) => {
            _db = client.db('airbnb'); // Target MongoDB Instance
            callback();
        })
        .catch((err) => { console.log(err); });
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)