Using free Mongo DB Storage-as-a-Service

Among the main NoSQL databases Mongo DB played a vital role to popularise the concept of NoSQL by providing free training courses, certifications and Storage-As-A-Service (SaaS). In this article we are going to look into how we can setup up a free account in MongoDB Atlas to create a Database and connect to it from Node.js application.

1. Creating a free account in MongoDB Atlas:

Use the official website to register for an account. : https://www.mongodb.com/cloud/atlas/register

Register for MongoDB Atlas.

Next step is to select plan to create your DB Cluster. We are going ahead with a Free Plan and in case you want to change it you can always upgrade.

Choosing the Free Cluster.

Now we have selected our plan with, next step will be to select the Cloud Provider and the Region that your cloud database going to be hosted in. There are few options to choose from verity of service providers like AWS, Azue etc. we are going ahead with the default AWS provider.

Selecting the Cloud provider.

Give few minutes till your clusters is been provisioned. When it is ready you should see something simile to the below screen. To get things started click on the Connect button.

Starting the Cluster0
2. Configuring the Database access rights:

Before we can simply connect our application, there are few security settings we configure:

  1. Add an IP address to Whitelist your connection. (For the time being, add your public IP address. later you can change it to the Prod. server IP address)
  2. Create a MongoDB User. (A temporary user account
  3. Choose a connection methods (In our case we will be connecting our Application)
    • Select the Driver
    • Select the Version
Step 1 & 2 Mongo Cluster

The configuration of the database driver and obtaining the connection String:

Step 3 Mongo Cluster
3. Connecting to the Database from your application.

We will be initiating a new Node.js project to test our MongoDB connection.

mkdir test_mongodb
cd test_mongodb
npm init

Install the MongoDB dependencies.

npm i mongodb

From Node.js try to access your Cloud Database.

const MongoClient = require('mongodb').MongoClient;

const URL = "mongodb+srv://kay:YOUR_PASSWORD@cluster0-dgahc.mongodb.net/test?w=majority";

const client = new MongoClient(URL, { useNewUrlParser: true , useUnifiedTopology: true });

client.connect(err => {
  const collection = client.db("test").collection("devices");
 // You can perform the DB operations here.
  client.close();
});
Reference:
Cite this article as: Greg Samuel, "Using free Mongo DB Storage-as-a-Service," in Cyber Memos, December 22, 2019, https://cybermemos.com/developent/javascripts/free-mongodb-cloud-storage-example/.

Leave a Reply

Your email address will not be published. Required fields are marked *