“Node.js is a JavaScript environment used as a free server environment that keeps running on the V8 engine and different operating systems like Windows, Linux, MacOS, etc. Because of its solitary essence, Node.js is best suited to non-blocking, task-driven data centers. It was created with genuine, push-based frameworks in the head, which are used for conventional sites and back-end API facilities. We will be using it to connect the MongoDB database with the cloud MongoDB server via the Ubuntu 20.04 Linux system in this article. So, let’s take a look now. Let’s start with the Linux system update for the successful implementation of this article. Thus, we have been trying the sudo apt-get command for update via the sudo rights. It may ask for the password of a currently working Linux user. You have to add the password to proceed.”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-1.png" data-lazy- height="53" src="data:image/svg xml,” width=”722″>

After updating your Linux system, you have to install the software-properties-common bundle that contains the set of different repositories used to install different software via the Ubuntu 20.04 shell. To install this, you need to try out this installation command with the apt-get keyword.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-2.png" data-lazy- height="108" src="data:image/svg xml,” width=”894″>

After the installation of necessary packages and modules for further installation, we will be moving towards the installation of an “npm” package manager for Nodejs first. To install it, try out the apt-get installation instruction along with the keyword “npm.” Add your password, if asked at the shell, and continue.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-3.png" data-lazy- height="129" src="data:image/svg xml,” width=”662″>

Tap “y” to continue moving forward in the processing upon being asked: “Do you want to continue?”.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-4.png" data-lazy- height="185" src="data:image/svg xml,” width=”993″>

Wait until the progress reaches 100 percent on the processing of this query. You can also try out the same command with the keyword “nodejs,” as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-5.png" data-lazy- height="104" src="data:image/svg xml,” width=”699″>

After the successful installation of the “npm” package, we will be installing the Nodejs JavaScript environment package on our Ubuntu 20.04 system with the “apt-get” package in the installation instruction. Make certain to indulge the “sudo” privileges to do so. Use the keyword “nodejs” in the command. It will be installed in no more than 2 minutes.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-6.png" data-lazy- height="109" src="data:image/svg xml,” width=”658″>

Let’s check out the “npm” version just installed at our end with the version command. So we have been using the keyword “npm” with the “-v” option and got version 6.14.4.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-7.png" data-lazy- height="109" src="data:image/svg xml,” width=”658″>

To check if the nodejs has been successfully installed in our system, we need to use the dpkg command with the –get-selections option. It will list down all the mounted packages.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-8.png" data-lazy- height="334" src="data:image/svg xml,” width=”752″>

Roll down a little, and you will see “nodejs” in this list.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-10.png" data-lazy- height="115" src="data:image/svg xml,” width=”735″>

It’s time to install “MongoDB” at our end with the use of the “npm” package at the shell. So, we have tried the installation command with the keyword “MongoDB” via the “npm” package. Wait until it collects the information and install it fully.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-11.png" data-lazy- height="52" src="data:image/svg xml,” width=”967″>

After the installation, it will show the following output, i.e., the installed version of MongoDB. It is also stating that 3 packages might require funding to fully configure them.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-12.png" data-lazy- height="754" src="data:image/svg xml,” width=”994″>

You can also try out the “npm” package within the list command along with the keyword “MongoDB” to display the versions of MongoDB installed by the “npm” package. Right now, we have only one version of mongo dB installed on our system, i.e., 4.6.0.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-13.png" data-lazy- height="364" src="data:image/svg xml,” width=”686″>

After the successful configurations of npm, nodejs, and mongo dB at the Ubuntu 20.04 Linux system, it’s time to set up the cloud server now. So, you need to open the MongoDB cloud site in your browser and register from the authenticated email. After the successful registration, log in from the cloud MongoDB and create a new cluster. You need to select the location while creating a cluster and label it. For instance, we have named it “test,” as you can see from the “All Clusters” page.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-14.png" data-lazy- height="429" src="data:image/svg xml,” width=”1568″>

Tap on the cluster name “test” to see its data and servers. You have to make a new user and its password for this cluster “test.” To connect the Linux machine with the cloud server, you need to add the IP address of your Linux machine to the Database Access panel and save it. We have created a user name with “saeed” and given it a new password. You need to create a new database in the cloud as well.

We have created a database “test” and added a new collection “Data” within it. Within the “Data” collection, we have added a single record for columns “id” and “title.” The shown-below output is all about your test server. Tap on the “connect” button shown on your below screen to connect this cloud server with the Linux system via Nodejs. You will be provided with 3 different ways to connect your cloud server with the local database, i.e., via terminal shell, via the application, and others. Copy the URL provided to connect a local database with the cloud server. Note that the URL will be different according to the Nodejs versions.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-15.png" data-lazy- height="467" src="data:image/svg xml,” width=”1576″>

Create a new JavaScript file using the touch instruction, i.e., node.js. Add the shown-below code within it. The variable “URL” must contain the copied URL from the cloud. Add your username and password for the “test” server within this URL provided and paste it. This JavaScript code will be used to connect the “test” database of MongoDB with the cloud and display the data of collection “Data” at the shell. If the connection got unsuccessful, it would throw an error.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-16.jpg" data-lazy- height="419" src="data:image/svg xml,” width=”854″>

If you have tried the above code for connectivity, run the node.js file at the shell using the “node” command as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-17.png" data-lazy- height="652" src="data:image/svg xml,” width=”985″>

You can also use the URL provided for the option of connecting through the MongoDB shell, as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/Nodejs-MongoDB-Find-18.png" data-lazy- height="131" src="data:image/svg xml,” width=”983″>

Conclusion

This tutorial briefly illustrates using the Nodejs to find the MongoDB cloud server data at the local server. You need to create an account on a cloud server, generate a new database and its collection, and add a username, password, and Ip address. After that, you need to use the URL provided at your cloud on the MongoDB shell or Nodejs file to connect. The database collection data will be displayed at the shell, i.e., added in the cloud database and shown on the local MongoDB shell.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/IMG-20211027-WA0029-150×150.jpg62a7f7e3e894f.jpg" height="112" src="data:image/svg xml,” width=”112″>

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.