MongoDB Atlas

There are several mongodb hosting services. MongoDB Atlas provides free hosting. To use MongoDB Atlas, we need to set up a free account.

You can set up your account at the link below:

https://www.mongodb.com/cloud/atlas/register

 

Once you have an account, you can create a free database. You should host your database in the Shared Tier Cluster. You should choose Belgium as your region. You can choose any provider. In the example below, the provider is Google Cloud.

 

 

 

MongoDB will only connect to IP addresses that are included in its Network Access. You will need to add your current IP address, as shown below.

 

 

To use mongoDB Atlas, we only need to change the host address of the database in the server/config/db.js file, as shown below

// localhost
mongoose.connect(`mongodb://localhost/${process.env.DB_NAME}`, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true})


// Mongodb Atlas
mongoose.connect(`mongodb+srv://${process.env.DB_USER_NAME}:${process.env.DB_USER_PASSWORD}@cluster0.uqtum.mongodb.net/${process.env.DB_NAME}`, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true})

 

The Mongodb Atlas username and password can be stored with the database name in the server/config/.env file, as shown below:

...

# Database
DB_NAME = your_mongodb_atlas_database_name_goes_here (eg use the same name as was used in the localhost, such as D00123456)
DB_USER_NAME = your_mongodb_atlas_user_name_goes_here (eg admin)
DB_USER_PASSWORD = your_mongodb_atlas_password_goes_here (eg 12£Qwe)  

...

Create a MongoDB Atlas account.

Create a MongoDB Atlas database.

Open the mongoDB project from the previous section in these notes. Change the code so that it connects to a MongoDB Atlas server.

"Cars" Worked Example

The full project code for the "Cars" Worked Example that is described below can be downloaded from this link.

Client-Side

No changes are needed on the client-side.

Server-Side

server/config/.env

# This file holds global constants that are visible on the Server-side

# Database
DB_NAME = D01234567
DB_USER_NAME = admin
DB_USER_PASSWORD = 123!"£qweQWE

# Port
SERVER_PORT = 4000


# Local Host
LOCAL_HOST = http://localhost:3000

When we stored our database on the local host, we did not need to provide a username and password. To access a mongoDB Atlas database, we need to provide a username and password.

server/config/db.js

const mongoose = require('mongoose')
//mongoose.connect(`mongodb://localhost/${process.env.DB_NAME}`, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true})

mongoose.connect(`mongodb+srv://${process.env.DB_USER_NAME}:${process.env.DB_USER_PASSWORD}@cluster0.uqtum.mongodb.net/${process.env.DB_NAME}`, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true})

const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', () => {console.log("connected to", db.client.s.url)})

We connect to the mongoDB Atlas server instead of localhost.

 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>