DEV Community

Cover image for MongoDB is a business choice
Israel Rotimi
Israel Rotimi

Posted on

MongoDB is a business choice

Yep, just discovered it.

In the ever-evolving landscape of web development, selecting the right database is crucial for both performance and scalability. As a frontend developer specializing in Next.js, ReactJS, and TailwindCSS, I recently embarked on integrating a backend into a dashboard application I initially built using a mock API. This journey led me to explore MongoDB, and the experience has been enlightening.

I've always considered myself a MERN stack developer although I had never actually touched MongoDB, that is, until now.

I just completed a task I was given as an assesment for a for a frontend job, (find out more about it here), where I built a dashboard app with React and pulled data from a mock API.
Since I was done with that I decided to build a backend for it and turn it into a portfolio project. That was how I dabbled into MongoDB

I used MongoDB atlas for my project. I went on to designing the Schema for my backend using mongoose and TBH, it's pretty flexible. Scalability is built in from the get go.
The fact that you can define your data in JSON-like structures is just awesome as it gives JavaScript devs a native feel.
Unlike in SQL DBs where you have to define tables for different datasets and then join the tables which can get complicated quickly if your application's data doesn't have much shape.

Schema Design: A Comparative Insight

In traditional SQL databases, structuring data for an application that includes users, blogs, comments, and likes would require multiple tables and complex JOIN operations. For instance, you'd need separate tables for users, blogs, comments, and likes, each with defined relationships and constraints.

In contrast, MongoDB's document-oriented approach allows for embedding related data within a single document. This means that a blog post can contain its comments and associated likes directly within its document structure. Such embedding reduces the need for JOIN operations, potentially enhancing read performance and simplifying data retrieval.

Say, for instance, my I have an App where users can write blogs and comment on blogs and rate them. In SQL DBs, I will need one table for users, one for blogs, one for comments:

CREATE TABLE Users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(255) UNIQUE NOT NULL,
    -- ... other user fields
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP
);

CREATE TABLE Blogs (
    id INT PRIMARY KEY AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    author_id INT NOT NULL,  -- Foreign key referencing Users
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP,
    FOREIGN KEY (author_id) REFERENCES Users(id)
);

CREATE TABLE Comments (
    id INT PRIMARY KEY AUTO_INCREMENT,
    content TEXT NOT NULL,
    user_id INT NOT NULL,  -- Foreign key referencing Users
    blog_id INT NOT NULL, -- Foreign key referencing Blogs
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES Users(id),
    FOREIGN KEY (blog_id) REFERENCES Blogs(id)
);

CREATE TABLE Likes (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL, -- Foreign key referencing Users
    comment_id INT NOT NULL, -- Foreign key referencing Comments
    createdAt TIMESTAMP,
    updatedAt TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES Users(id),
    FOREIGN KEY (comment_id) REFERENCES Comments(id)
);
Enter fullscreen mode Exit fullscreen mode

Then I will have to link all of them together using Joins.
Not bad, but in MongoDB:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
  username: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true }, // Securely hash this in your app
  blogs: [{
    title: { type: String, required: true },
    content: { type: String, required: true },
    comments: [{
      content: { type: String, required: true },
      user: { type: Schema.Types.ObjectId, ref: 'User', required: true },
      likes: [{ user: { type: Schema.Types.ObjectId, ref: 'User' } }],
      timestamps: true
    }],
    timestamps: true
  }]
}, { timestamps: true });

const User = mongoose.model('User', userSchema);

module.exports = { User };
Enter fullscreen mode Exit fullscreen mode

Why MongoDB Stands Out

Several features make MongoDB a compelling choice for developers:

  • Flexibility: MongoDB's schema-less design accommodates evolving data models without the need for extensive migrations.

  • Scalability: Built-in sharding and replication support horizontal scaling, ensuring the database can handle increased load as applications grow.

  • Performance: Storing data in BSON (Binary JSON) format allows for efficient data retrieval and manipulation, which is particularly beneficial for applications requiring high throughput.

  • Developer-Friendly: The alignment with JSON and JavaScript ecosystems streamlines the development process, reducing the cognitive load when switching between frontend and backend development.

Conclusion

Integrating MongoDB into my project has been a transformative experience. Its alignment with modern JavaScript frameworks and its flexible, scalable nature make it an excellent choice for developers looking to build robust applications efficiently. For those considering a transition from traditional relational databases or starting new projects, MongoDB offers a compelling blend of performance and ease of use.

Please share your thoughts and opinions in the comments below.

*Follow me on LinkedIn: https://linked.com/in/israel-rotimi and check out my other posts. I'm open to Job offers and freelance work. Feel free to contact: izzyrotimi@gmail.com *

Top comments (0)