MongoDB database for data storage | Express-Mongo integration | Compass | Full stack web development

Опубликовано: 11 Февраль 2025
на канале: Shaheer Shukur
6,657
82

Source Code: https://github.com/shaheershukur/Mark...
Complete Playlist:    • Web Application Development  

We will use MongoDB to store and retrieve data to microservices, and then to UI ; instead of using previously hardcoded data in microservices.

Closed Captioning:
We have User Interface, and microservices ready.
In this video, we will add the MongoDB database to the project.

Currently we have hard coded the response JSON in express microservice.
In real scenarios, we must be storing all the data in Database tables,
microservice will be fetching data from database and creating JSON Object,
and finally UI will receive this JSON object and display it to the user.

Let's get started.
First, we need to install MongoDB Community Server from their official website.
Select the complete installation.

Once the installation is completed, we can open MongoDB Compass.
Compass is a easy to use graphical user interface of MongoDB.
We will come back to this later.

Before that, we need to set up few more things.
First, we need to add the MongoDB installation folder to environment PATH variable,
so that we can run MongoDB commands from anywhere, in the terminal.

Next, we need to create a folder named db inside data folder in C drive.
This is the default path were MongoDB will be creating Database and storing data.
It is also possible to change this DB path if required.

Let's test whether MongoDB is correctly installed and working.
Type mongo -version in cmd.
We can see the installed MongoDB version displayed.
Now let's run the MongoDB daemon, by typing mongod.

The MongoDB daemon started running.
We can either run daemon in cmd, or in VS Code terminal.
Daemon is the primary process for MongoDB system.
When it starts running, it manages everything, and handles client requests coming to the Database.
The client request can made from cmd by running mongo command,
or using MongoDB Compass,
or using our web application.
For setting up data, we will use MongoDB Compass.
And for fetching and displaying data in UI, we will use express server of our web application.

Let's implement database to our project.
So far, we have been sending a hardcoded JSON response from Express microservice to the UI.
Now we are going to store these values in database, and fetch it from there, whenever required.

Before we jump in, let's understand some basic terminologies.
In MongoDB database, a table is called 'collection'.
and a row in a table is called 'document'.

Open MongoDB Compass.
From here, we will connect to the MongoDB daemon.
By default, daemon will be running on port 27017 of localhost.
So we need to provide the connection string like this.
We are successfully connected to MongoDB.
here we can see all the MongoDB databases.
To create a new database for our web application, click on the 'Create Database' button at the bottom.
We will name the database 'market', and we will name the collection 'DEALS'.

The collection 'DEALS' is empty.
We can either insert documents manually, or we can import documents from a file.
Let's add data from file.
For that, we will create a new folder 'market-db', and a JSON file in it.
In this file, we need to write an array, containing all the documents to be inserted to the collection.

If we look at the expected response, we can see that 'image name' and 'title' attributes are duplicated, and column and row attributes are varying.
Hence, we can design the collection like this.
Since JSON file do not support single quotes, we will replace it with double quotes.
Also, all the JSON keys must be enclosed within double quotes.

Let's insert these objects into our collection.
We can see 4 documents inserted into our collection.
It is currently displayed as a list.
We can also view it as objects, or in tabular form.
You can also see that, MongoDB automatically added unique IDs to all the inserted documents.
Data is set up at DB side.
Now we need to fetch the data from MongoDB to Express microservice.

We will comment out this code.
And we will call a function here to fetch the data from Database.

For that, let's create a Database abstraction layer.
We will create the file in javascript folder.

Since we are going to connect to MongoDB, we must first install it using npm.
We will then import it in our file.
Using the 'connect' method of MongoClient class, we can establish a connection to the Database.
We must pass the 'connectionString' parameter to it.
Also, we must set 'Unified Topology' to true.

The connect method returns a Promise.
Hence we need to chain it with 'then' and 'catch'.
In case the connection was successful, we will set a variable to 'true'.
Also, we will assign our collection to a global variable 'db'.

Now we will write a function 'queryDealsCollection', which will be called from the 'deals' router file.
If the connection was successful, we will process the data, else we will return null.
This is the JSON structure we need to return.