Getting started with mongodb atlas and nodejs

Hi All

our focus today is on MongoDB atlas and nodeJS.

We are going to create and store our data in the cloud using MongoDB atlas database.

MongoDB atlas in a cloud based data base where one can store and retrieve his/her data securely.

NodeJS is this case is used to provide an interface for connecting our database in the cloud.

The cloud instance here means that we will not be storing our data locally but instead we will be storing it in the web,

so one needs an internet connection in order to access the data in the data base.

One of the good cloud based database is MongoDB.

Les get started

So am going to break this tutorial into parts.

  • The first part we are going to create an atlas account
  • The second part we are going to Deploy a free cluster
  • The third part we are going to add a connection IP address
  • The fourth part we are going to create a database
  • The finally the last part we are going to connect our database with our application

I BE WILL USING SCREENSHOTS FROM MY END FOR CLARITY

PART ONE: CREATE AN ATLAS ACCOUNT

To create an atlas account head over to https://account.mongodb.com/account/register?tck=docs_atlas

Then register using your working email address then fill in the details.

Then login to your email address used and verify the registration process.

After the verification process login to your account using this https://account.mongodb.com/account/login

PART TWO CREATE A FREE CLUSTER

After the login process you will be redirected to a page where you you will build you cluster.

Click create a cluster then click on Shared cluster plan/ free option as shown in the image below.

shared option
  • click on create Then choose your cloud provider and region

In my case i have aws as my cloud provider and North America > N.Virginia as my region

CREATE

region

After the successful creation you will be redirected to this page,

*Give it some time to create

cluster

PART THREE: ADD YOUR CONNECTION IP ADDRESS

Click on network access then add an IP address

IP address
  • Click on Allow access from anywhere then confirm

allow access
  • Upon successful addition you should see this, the status should read active

active

PART FOUR : CREATE A DATABASE

In this part we are going to create our database where we will store our data

to create head over to the section named CREATE your first database

  • Click connect
  • Fill in your username and password then create your database user

PART FIVE : CONNECTING TO YOUR LOCAL WORK

In this part we are going to use nodeJS and Express to connect our application

  • This part is divided into two parts.

part a we are going to create a node application,

Then part b we are going to connect our application to atlas MongoDB.

  • Here to need to install the nodeJS in you Computer

PART FIVE a CREATING A NODEJS APP

To create a nodeJS app

  • open the terminal(command line) and verify whether nodeJS is installed on your pc.
  • type node -v in my case i have the following output in my terminal.
ian@IanoTech:~$ node -v
v10.19.0

on your PC navigate to the directory of your choice.

create a folder(project) named mongodb

note that the directory can either be Documents,pictures,music Desktop

  • Now On the command line change the directory by typing
  • cd directory navigated to then cd name of folder created

In my case i have documents is my suitable directory then navigate to MongoDB.

ian@IanoTech:~$ cd Documents
ian@IanoTech:~/Documents$ cd mongodb
ian@IanoTech:~/Documents/mongodb$ 

The next task now is to initialize our project using npm init -y .

On the command line type npm init -y.

ian@IanoTech:~/Documents/mongodb$ npm init -y
Wrote to /home/ian/Documents/mongodb/package.json:

{
  "name": "mongodb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}


ian@IanoTech:~/Documents/mongodb$ 

Now open your project named mongodb using the suitable code editor e.g sublime text,vs code etc

In my case am using sublime text.

init
  • On the root folder of the project create a file and name it server.js.

Then install express by typing the following command on the terminal

npm i express.

ian@IanoTech:~/Documents/mongodb$ npm i express
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN mongodb@1.0.0 No description
npm WARN mongodb@1.0.0 No repository field.

+ express@4.17.1
added 50 packages from 37 contributors and audited 50 packages in 18.921s
found 0 vulnerabilities

Lets create our server now,

Add the following lines of code to your server.js file created.

our app is going to run on port 3000.

const express = require('express')
const app = express()
const port = 3000

app.listen(port, ()=> console.log(`running on ${port}`))

To run our app type node server.js on the terminal

a message running on 3000 will be dispalyed

ian@IanoTech:~/Documents/mongodb$ node server.js
running on 3000

Good our app is up and running

PART FIVE b

Connecting our app

Once you created your database you were redirected to a page.

On the page first choose a connection method,

then secondly Click choose a connection method and then choose Connect your application ,

then finally Once on the connection page copy the string that will be generated and store it somewhere safe.

in my case this is my string

mongodb+srv://ian:<password>@cluster0.wfyg5.mongodb.net/myFirstDatabase?retryWrites=true&w=majority

Now in your app create an app named connectionString and paste the string generated

const connectionString  = 'mongodb+srv://ian:<password>@cluster0.wfyg5.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'

POINT TO NOTE REMEMBER TO REPLACE YOUR PASSWORD AND DATABASE NAME CREATED

In the places named <password> and myFirstDatabase in your string.

To connect to our database we need to install mongoose – a package connector.

to install type npm i mongoose on the terminal.

Lets first import mongoose on our server.js file.

const mongoose = require('mongoose')

Then connect using the code below.

mongoose.connect(
	connectionString,
	{ useNewUrlParser: true, useUnifiedTopology: true,useCreateIndex: true  }

	)
.then(()=> console.log('Connected successfully'))
.catch((err)=> console.log(err))


now lets run our app by typing node server.js on the terminal.

Once its running you will see the following message on the command line.

ian@IanoTech:~/Documents/mongodb$ node server.js running on 3000 Connected successfullywell done

MISSION COMPLETED on Getting started with mongodb atlas and nodejs

 

Posted in
programming

Post a comment

Your email address will not be published.

As the top software development company in Kenya, we can't wait to show you what we can do. Our team of talented designers and developers are passionate about creating beautiful, functional websites that not only look great, but also help your business grow.
0

No products in the cart.

× Get Help?