A Guide to Setting up a MongoDB Cloud Database

Tish Faroul
5 min readAug 28, 2022

--

In an effort to expand and keep growing my knowledge, I’ve taken up learning Node.js to further build my backend skills.

If you’ve dipped your toes into the backend, I’m sure you’ve come across references to MongoDB.

https://www.mongodb.com/

What is MongoDB?

MongoDB is a NoSQL database — meaning that it uses collections and documents to keep track of data.

Unlike SQL databases, it doesn’t use schemas and it accepts JSON documents.

Note the differences between the two types of databases

Each collection is used to store a particular type of data/document.

In my project (which I’ll detail later), I created a Collection of Posts.

In this collection, there are several Post documents (with each document populating with a unique ID).

Why should you use it?

It makes development quicker than using a SQL database, as the way the documents are structured and designed is less stringent.

Using a Cloud Database

For my first foray into using MongoDB, I chose to create a cloud database instead of downloading a local version of it to my computer.

This saves me the trouble of figuring out how to deploy the database later on.

We’ll be using MongoDB Atlas

https://www.mongodb.com/atlas/database

It is free! (my favorite word), so sign up and create your first cluster

(A cluster allows you to scale reads and writes across several nodes)

How to set up your Database

  1. Create a cluster

2. Choose a provider

Leave the default settings (AWS as the cloud provider and whatever region you’re in) and name your cluster

3. Create a Database and Collection

Within your cluster — hit collections and add your own data.

Now, it’s time to name your database and collection.

I’ve named my database ‘Node-blog’ and my collection ‘Posts

Below, is our Database and Collection of Posts (which is currently empty, but we’ll fix that later)!

Now that we’ve created our Database, it’s time to create a user that will be allowed Database access.

4. Set Database Access

Under Security, click ‘Database Access’, then add a new user

You are given three authentication options.

In my humble opinion, password authentication is the easiest option.

Name the user and set a password for the user.

Below, you will see that I have it set so that the user has both read and write access to my database as I will later have my app perform CRUD actions.

5. Connect to your app

Finally, it’s time to connect your database to your project.

Go back to your clusters and hit ‘Connect’, then ‘Connect your application’

Below, you will see that we’re provided a connection string.

Note: If you want to exit test mode, insert the name of your database before the ? in the connection string)

Copy and Paste the connection string to your app.js (or wherever you set up your database logic in your project)

You will need to replace ‘<password>’ with the password you created in the previous step.

At this point, we have the option to connect to the database using the MongoDB API package or by using Mongoose.

What is Mongoose?

Mongoose is an Object Document Mapping (ODM) library. It wraps itself around the MongoDB API and creates data models, schemas, and allows you to make requests to your database using defined query methods (linked here)

6. Set up Mongoose

If you use node package manager — simply run ‘npm install mongoose’ in your terminal.

Require’ mongoose like below:

Now, we can use this mongoose object to connect to the database, using the ‘connect’ method:

Note: ‘dbURL’ that I passed in here, is my connection string from step 5

This returns a promise, so now we can add a then method and tell our app to listen for requests on Port 3000 (or whichever you prefer).

7. Create a schema & model

Within my project, I created a new folder called ‘models’, and created a file within it called ‘posts’.

Within the ‘posts’ file, I:

a. ‘Require’ mongoose

b. Get the schema from the mongoose object.

c. Name my schema (I will name mine postsSchema) and make it equal a new instance of a schema object.

d. Describe how you want your document to be modeled within the new schema object.

Here’s what I have:

Now, we will create a model like this:

It uses a method called ‘model’ that takes in the name of the model as the first argument. The second argument is the schema we created.

Finally, we export our model so that it can be used throughout our project.

8. Test our setup by performing a Get Request

We will first import the model we created in the previous step into our app.js file.

Now, we will perform a ‘Get’ request.

We will use the model we created to create a new instance of a Post document and save it to our collection using the ‘save’ method.

Note: I forgot to include the ‘body’ in my new post, but it still saves

Now, when I go to the ‘localhost:3000/new-post’ URL in my browser, I get the below JSON object:

If I go to my database on MongoDB Atlas, I will see that my document is in my collection:

It worked!

✨✨✨And that’s it!

Tish⚡️🎧🌙

--

--