mLab: Cloud Database

Formerly known as MongoLab, mLab is a manageable database hosting service in the cloud. A MongoDB (NoSQL DBMS type) database that runs on AWS (Amazon), Google Cloud, and Microsoft Azure. It offers 500 MB free of charge for data storage.

We’ll learn how to create a database in the cloud and how to access it and enter data through a client application.

Create a database in the cloud

We need to perform the following steps to create a remote database:





You are now ready. You have created the remote database, and you will be able to see it in the dashboard.


Clicking on the name of the created database will take you to a detailed page with all the connection information which we will use later in our application. Further down the line, we will create Collections, Users, Stats, Backups, and Tools.


For now, we will create a User by clicking on Add database user.


Create a client application and access the database

Now that we have created our database we will access it through a Node.js application. Create a Node.js project and add the index.js file in the folder.

const express = require('express');
const bodyParser = require('body-parser');

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

//Connection to remote database tutorial-test
MongoClient.connect('mongodb://', (err, client) => {
    // Client returned
    var db = client.db('tutorial-test');
    if(!err) {

      //Create a collection of Students
      db.collection('Students', function (err, collection) {
        //Insertion of 4 students in the collection      
        collection.insert({ name: 'Finbarrs', age: '18' });
        collection.insert({ name: 'Justin', age: '22' });
        collection.insert({ name: 'Rita', age: '23' });  
        collection.insert({ name: 'Sean', age: '19' });    

      db.collection('Students').count(function (err, count) {
            if (err) throw err;            
            console.log('Total rows entered: ' + count);

In the code above, you will notice we created an instance of MongoClient and connected to the remote database through the connection string. Do not forget to replace the username:password with yours.

Likewise, a collection named “Students” was created, and we inserted four lines with the name and age of the students.

Finally, we returned the total number of rows in the console. After inserting the above code, use NPM to download MongoDB package latest version in the project folder:

npm i mongodb

You can execute the code with the npm start command on the terminal. We will see that four lines were inserted in the Students Collection.


Let’s go back to our database on the mLab’s website. You will see in the Collection tab that the “Students” table was created and the lines were inserted with the name and age of the four students.


It is pretty neat, huh? Of course, this is a simple example of how you can use your client application to communicate with a cloud database like mLab.

mLab documentation is here, and you can check out the repository of this tutorial on GitHub.