MongoDB Atlas for a SaaS
Tags: mongodb, database, javascript, webdev
Published: 2021-11-15 11:01:35

I decided to build a SaaS last month called Enhance AI, using OpenAI with GPT-3 to perform some interesting applications of AI to help programmers save time coding.

I needed to pick a good database for this SaaS. As the title suggests, I ended up picking MongoDB, however how I came to this conclusion took some research.

{% youtube 14pAPdAowYc %}

Youtube Video: <a href="https://youtu.be/14pAPdAowYc">Creating a SaaS Database</a>
The SaaS: <a href="https://www.enhanceai.ai">Enhance AI</a>
MongoDB Atlas: <a href="https://www.mongodb.com/cloud/atlas/register?utm_campaign=adrian_twarog&utm_source=youtube&utm_medium=influencers&utm_content=saas_database_in_7_days">https://www.mongodb.com/cloud/atlas/</a>

(you can use the promo code Adrian100 to get extra free $100 of credit when signing up!)

## Introduction

This will cover what I learned by picking and implementing MongoDB Atlas for my project and my thoughts. If you're expecting something technical, this won't be for you.

* Defining the scope and requirements
* Picking databases
* Deciding local vs cloud database
* Setting up the database schema
* Creating the API routes

![Mongodb Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/28w6of5vdfz50bo4gx7c.gif)

## Defining the Scope & Requirements

The first thing you want to consider is your goal for a project. Mine was to build a startup, in 30 days or less. This means I needed to build fast, and have good documentation whenever I encountered problems.

There was lots of options like firestore, or mysql, however one of the most common startup stacks is called MERN, which is Mongo, Express, React and Nodejs. There are lots of youtube videos on how to set them up (now one more!) and I was quite confident as I had worked with each one of those technologies in the past.

The other requirements was working with JSON objects, which Mongo does quite well as because of the fact that it's a document/collection based database. And finally the fact that it's quite flexible in the fact that it's schema can easily be changed on the fly meant I believe it was a good choice for my startup.

![Mongodb Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tz440c5dqs0zzgc7fx2.gif)

## Deciding local vs cloud databases

It's 2021 and soon to be 2022. You are no longer limited to setup an entire production workstation with all items installed, such as a web server, database, files and more. Microservices and cloud services now allow you to distribute all your technologies.

So the question of hosting the database locally or online is an actual decision to make. There are a few advantages and disadvantages to both approaches. Yes you have more control if everything is locally on your machine, but by comparison, having a cloud version of a database means it's accessible anywhere.

As I often swap computers, work remotely, and want more flexibility, I had a look at the cloud version of MongoDb. It's called MongoDB Atlas. I have the option to connect directly to it via my vscode with a plugin, which is quite useful. On top of that, you can scale and grow it with your needs.

Other than that, the setup is exactly the same for connecting it to your project, minus the requirement to install and actually host the database on the machine. This meant I got to save time and effort and simply copy paste the url of the database from Mongo and I was up and running in minutes.

![Mongodb Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/durn1081rk4ein00agct.gif)

## Setting up the Database Schema

Every website and application (hopefully) is unique. However there are some items which remain the same, such as the requirement for users, authentication, etc. I took a few days to plan out how I wanted to structure my database before actually implementing it.

The main difference was the fact that I was using OpenAI, and I wanted to make sure that could help improve my GPT-3 outputs overtime, so I made sure to also implement a history schema. Using express, I setup mongoose as it's one of the easier ways to define a schema, which I could later use in my routes.

![Mongodb Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j6iwlozn0cd5q00woo52.gif)

## Creating the API

With the database picked, the cloud setup, the schema defined, I was able to implement the API with ease. 

Most of the requests could be simple GET, POST requests which are then using the findOne command using mongoose with mongo no different to if I was using it locally.

I originally thought I might get some significant delays by using the cloud version of MongoDB, however the server was quite fast and I never had any such delays.

## Conclusion

While this was a brief overview of using MongoDB Atlas, it's worthwhile that people try it over the alternative of running up a database locally and manually. You will get the same results with less time and have more flexibility moving forward.

I've done a while video which goes more into depth about this journey but hopefully this gives you a good idea of some of the items I learned during this time.

MongoDB Atlas: <a href="https://www.mongodb.com/cloud/atlas/register?utm_campaign=adrian_twarog&utm_source=youtube&utm_medium=influencers&utm_content=saas_database_in_7_days">https://www.mongodb.com/cloud/atlas/</a>

Youtube Video: <a href="https://youtu.be/14pAPdAowYc">Creating a SaaS Database</a>

{% twitter 1456518992781144067 %}

## Follow and support me: 
Special thanks if you subscribe to my channel :)
* [🎞️ Youtube](https://www.youtube.com/channel/UCvM5YYWwfLwpcQgbRr68JLQ?sub_confirmation=1)
* [🐦 Twitter](https://twitter.com/intent/follow?screen_name=adrian_twarog)