MongoDB Installation - A Quick Guide

Prasanna Lakshmi

Prasanna Lakshmi

Software Engineer

Published Mar 12 2019

About a Database

Database is a physical container of collections. Each database gets its own set of files in a file system. A single MongoDB server typically has multiple databases.

MongoDB - An overview

MongoDB is an open source, cross-platform, and document oriented database that provides high performance, high availability, and easy scalability in the realm of NoSQL databases. MongoDB works on the concept of collection and documents.

Collection

Collection is a group of MongoDB documents. It is the equivalent of tables in RDBMS. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are for similar or related purposes.

Documents

A document is a set of key-value pairs. Documents have dynamic schema, which means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

The following table shows the relationship of RDBMS terminology with MongoDB.

RDBMS MongoDB
Database Database
Table Collection
Tuple / Row Document
Column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by MongoDB itself)
Database Server and Client
MySQLd / Oracle mongod
mySQL / SQL*Plus mongo

Getting started with MongoDB using mLab

mLab - mLab is a fully managed cloud database service that hosts MongoDB databases. It offers FREE services for the beginners to practice MongoDB queries. However, it charges additional cost if you need a Production-like environment.

1. Existing users can sign in with their login credentials and new users can sign up at mlab.com.

MLab Login

2. Once you have created an account at mlab.com, an email verification issent to your registered email id.

MLab account verification

mLab eMail Verification

3. To complete the email verification process, click the verification link received at your registered email address. It will redirect you to mLab’s Home Page for confirmation and prompts you to select options for MongoDB Deployments and Environments.

Home Page

4. Click the “+ Create new” button next to MongoDB Deployments. On the next page select “Google Cloud Platform” under Cloud Provider. Now, select “SANDBOX” under Plan Type, and then click Continue.

Deployments

5. To proceed further enter “DATABASE NAME” under Final Details and then click “CONTINUE”.

Final Details

6. To confirm the details, click “SUBMIT ORDER” under Order Confirmation.

Order Confirmation

7. Now, the new database is created under MongoDB Deployments.

MongoDB Deployments

8. Click the Database name and then “Users” tab and then Click “+Add database User” to create your desired login userid and password.

Database

New Database User

Once the user id is created, save the MongoDB host ID and port values provided on the Home page(highlighted below) to connect from shell or any user interface of MongoDB, like RoboMongo or Robo 3T (UI).

Database User ID

9. Once the user id is created for a database, Click the “Collection” tab and then Click “+Add Collection” to create a new collection called “ProductMaster”. To confirm the details, click “Create

Database Collections

10. Install RoboMongo via Google (https://robomongo.org/download)

RoboMongo

11. Once RoboMongo is installed, click “Create” in the MongoDB Connections that appears as a new window.

RoboMongo Connection

12. Once the connection window is displayed, user can try to connect to the MongoDB that was created earlier in mLab.

MonogoDB Connection

Connection Settings

Authentication

13. After entering all the connection details, click “Test” button to validate the connection details.

Diagnostic

14. As the database is connected to expand the DB collections on the left side menuto find the “ProductMaster” collection that we created. Now, user can execute the command to “SELECT” rows[documents] from the “ProductMaster” collection and then click “Green Run” button b.getCollection('ProductMaster').find({})

ProductMaster

15. Insert a row [document] in ‘ProductMaster” Collection by running the command mentioned below that is,

db.ProductMaster.insert({ ProductId: "100001", ProductName : "Bracelet", Size : "XL", Color : "Red"})

ProductMaster Collection

db.ProductMaster.insert({ ProductId: "100002", ProductName : "Watch",Color : "Brown"})

ProductMaster Insert

16. Now,user can see the two documents ready to be inserted into the collection.

ProductMaster Document

Mongo DB Operation Query
SELECT all rows from a Collection db.getCollection('ProductMaster').find()
Count no. of rows in a collection db.ProductMaster.count()
Find a specific product (WHERE) db.getCollection('ProductMaster').find({ProductId: "100002"})
Find list of products (IN clause) db.getCollection('productmaster').find({ProductId: {$in: ["100001","100002","100003"]}})

NOTE: The table and column names must be mentioned in right cases as MongoDB is case sensitive.