CRUD Operations with Express, Node, and MongoDB.

Oremei Akande
4 min readAug 21, 2020

--

The acronym CRUD stands for create, read, update, and delete. In the world of computer programming, each letter stands for all functions represented in a relational database. It also represents the basic ways users interact with applications. These interactions vary from users creating an account, viewing chats, deleting posts, uploading profile pictures, e.t.c.

So, if you are still here I believe you are interested in carrying out these operations and persisting data in MongoDB using mongoose orm. Quickly we will be doing the following:

  1. Setup and connect to MongoDB ATLAS
  2. Download npm packages, set up an express app
  3. Write basic crud operations for a notification app

Setup MongoDB ATLAS

In the next steps, we will be looking at setting up a Mongo database on Atlas. If you are far gone this stage you can move on to writing crude operations using mongoose.

Ever wondered where data shared on your everyday applications are stored? Let us just say the data are kept in a house called databases. One of such database is MongoDB. Let us set one up. Create an account on mongo atlas. When logged in, create an Organization and set a cloud service. In this case, MongodbAtlas

Name your organization
Select Cloud Service: MongoDB Atlas

Click the ‘next ’button and then the ‘create organization’ button. You should see a screen as below:

Okay, so where is the database? It is right a few steps away. How about you creating a project first? By clicking on the ‘New Project’ button. Yous should see this:

Enter the name of your project and click on the ‘Next’ button.
You are already added as a member so go ahead and click the ‘Create Project’ button.

You should see this page below on your screen:

Click on ‘Build a Cluster’ You should the screen below:

Click on ‘Create a cluster’ on this screen for the free version. Also, click on ‘Create Cluster’ on the next screen. You will have to wait for approximately 5 minutes for the cluster creation. You will see this when the cluster is ready.

Cluster created!

The next point of action is to connect to the MongoDB Atlas.

Click on ‘CONNECT’ a modal should pop up. Whitelist your IP address so as to connect to your cluster. Simply click on ‘Add Your Current IP Address’

Create a database user:

Next, click on ‘Choose a connection method’

Select ‘Connect your application’ and copy the connection string.

Click ‘Copy’ to copy connection string.

The connection string should look like this:

mongodb+srv://spect<password>@cluster0.dqznw.mongodb.net/<dbname>?retryWrites=true&w=majority

Replace <password> with your password and <dbname> with your database name.

Packages download and connecting with our database from the app.

I am assuming you already have Node installed on your system. Go to your command terminal on your favorite text editor and type the following commands.

npm init: Hit enter though everything that appears. This creates a package.json file that manages all dependencies we will download as we continue.

Create an index.js file in the root of your app. This is your entry file.

Next, we have to install express and mongoose as below

npm install express mongoose dotenv nodemon body-parser

Require express, mongoose, and connect to a PORT. In this case, 8085

If I run this app, using nodemon index. It will be like this:

In the next tutorial, we will connect our express app with the MongoDB Atlas and write basic crud operations.

Click here for the second part of this tutorial

Thanks for reading.

--

--