MongoDB Introduction and Installation

MongoDB Installation on Ubuntu Server using AWS EC2 Instance

Introduction

MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like relational database structure but provides an altogether different mechanism for the storage and retrieval of data. This format of storage is called BSON ( similar to JSON format).

{
  title: 'HashNode',
  by: 'Ashutosh Kurwade',
  url: 'https://ashutosh25.hashnode.dev/',
  type: 'NoSQL'
}

SQL databases store data in tabular format. Relational Database Management System(RDBMS) is not the correct choice when it comes to handling big data by their design since they are not horizontally scalable. If the database runs on a single server, then it will reach a scaling limit. NoSQL databases are more scalable and provide superior performance. MongoDB is such a NoSQL database that scales by adding more and more servers and increases productivity with its flexible document model.

RDBMS vs MongoDB:

  • RDBMS has a typical schema design that shows several tables and the relationship between these tables whereas MongoDB is document-oriented. There is no concept of schema or relationship.

  • Complex transactions are not supported in MongoDB because complex join operations are not available.

  • MongoDB allows a highly flexible and scalable document structure.

    For example, one data document of a collection in MongoDB can have two fields whereas the other document in the same collection can have four.

  • MongoDB is faster as compared to RDBMS due to efficient indexing and storage techniques.

  • There are a few terms that are related in both databases. What’s called a Table in RDBMS is called a Collection in MongoDB. Similarly, a Row is called a Document and a Column is called a Field. MongoDB provides a default ‘_id’ (if not provided explicitly) which is a 12-byte hexadecimal number that assures the uniqueness of every document. It is similar to the Primary key in RDBMS.

Features of MongoDB:

  • Document Oriented: MongoDB stores the main subject in a minimal number of documents and not by breaking it up into multiple relational structures like RDBMS. For example, it stores all the information of a computer in a single document called Computer and not in distinct relational structures like CPU, RAM, Hard disk, etc.

  • Indexing: Without indexing, a database would have to scan every document of a collection to select those that match the query which would be inefficient. So, for efficient searching Indexing is a must and MongoDB uses it to process huge volumes of data in very less time.

  • Scalability: MongoDB scales horizontally using sharding (partitioning data across various servers). Data is partitioned into data chunks using the shard key, and these data chunks are evenly distributed across shards that reside across many physical servers. Also, new machines can be added to a running database.

  • Replication and High Availability: MongoDB increases the data availability with multiple copies of data on different servers. By providing redundancy, it protects the database from hardware failures. If one server goes down, the data can be retrieved easily from other active servers which also had the data stored on them.

  • Aggregation: Aggregation operations process data records and return the computed results. It is similar to the GROUPBY clause in SQL. A few aggregation expressions are sum, avg, min, max, etc.

Where do we use MongoDB?

MongoDB is preferred over RDBMS in the following scenarios:

  • Big Data: If you have a huge amount of data to be stored in tables, think of MongoDB before RDBMS databases. MongoDB has a built-in solution for partitioning and sharding your database.

  • Unstable Schema: Adding a new column in RDBMS is hard whereas MongoDB is schema-less. Adding a new field does not affect old documents and will be very easy.

  • Distributed data: Since multiple copies of data are stored across different servers, recovery of data is instant and safe even if there is a hardware failure.

Language Support by MongoDB:

MongoDB currently provides official driver support for all popular programming languages like C, C++, Rust, C#, Java, Node.js, Perl, PHP, Python, Ruby, Scala, Go, and Erlang.

MongoDB Installation on Ubuntu Server using AWS EC2 Instance

Installing MongoDB:

How to Install and Configure MongoDB in Ubuntu using AWS EC2 Instance?

Step 1: Visit AWS Management Console.

Step 2: Log in to the AWS Management Console.

Step 3: Click on EC2 Service.

Step 4: Click on Instances.

Step 5: Click on Launch Instance.

Step 6: Then Give the Name and Tags. eg.MongoDB. Then Select the Ubuntu from Application and OS Images (AM). Then Select the Ubuntu Server, 20.04 LTS (HVM), SSD Volume Type from Amazon Machine Image (AMI). Then Select the t2.micro from the Instance type. Then Create new key pair from the Key pair(login). eg. mongo. Then download the key pair pem file automatically i.e. mongo. pem. Then Select Allow SSH traffic from Anywhere from Network settings. Then set all configurations by default.

Step 7: Then Launch instance.

Step 8: Open the XSHELL 7(command line terminal). The download link is here...

Step 9: Then again open the instance which is created above select the instance and copy the Public IPv4 address of that instance.

Step 10: Paste the Public IPv4 address of the instance to XSHELL 7 for connecting the server.

Step 11: Then Accept and Save then Enter the user name to log in. i.e. ubuntu Then Add the user key which is created above i.e. mongo.pem if required give the passphrase i.e. mongo Server Connection is established successfully.

Now, the actual installation of MongoDB is started on the Ubuntu server. Follow these steps to install MongoDB Community Edition using the apt package manager. link...

Step 12: From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

Issue the following command to import the MongoDB public GPG Key:

curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor

Step 13: Create the list file /etc/apt/sources.list.d/mongodb-org-6.0.list for your version of Ubuntu.

Click on the appropriate tab for your version of Ubuntu.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

Step 14: Issue the following command to reload the local package database:

sudo apt-get update

Step 15: You can install either the latest stable version of MongoDB or a specific version of MongoDB.

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

It, All done Installation.

  • Now, Start MongoDB.

You can start the Mongod process by issuing the following command:

sudo systemctl start mongod

Verify that MongoDB has started successfully.

sudo systemctl status mongod

  • Stop MongoDB.

As needed, you can stop the Mongod process by issuing the following command:

sudo systemctl stop mongod

  • Restart MongoDB.

You can restart the Mongod process by issuing the following command:

sudo systemctl restart mongod

  • Begin using MongoDB.

Start a mongosh session on the same host machine as the mongod.

mongosh