All about MongoDB
Introduction
What is a database?
A database is an organised collection of structured information, generally stored and accessed electronically from a computer system.
Different types of databases:
- SQL Database
2. Non-SQL Database
Introduction to MongoDB
MongoDB is an open source database management system. It uses document-oriented database model which supports various forms of data.
MongoDB Features
High Performance
MongoDB provides high performance. Most of the operations in the MongoDB are faster compared to relational databases.
Auto Replication
MongoDB provides auto replication feature that allows you to quickly recover data in case of a failure.
Replication is the process of synchronising data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers. Replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup.
Horizontal Scaling
Horizontal scaling is possible in MongoDB because of sharing. Sharding is partitioning of data and placing it on multiple machines in such a way that the order of the data is preserved.
Horizontal scaling vs vertical scaling:
Vertical scaling means adding more resources to the existing machine while horizontal scaling means adding more machines to handle the data. Vertical scaling is not that easy to implement, on the other hand horizontal scaling is easy to implement. Horizontal scaling database examples: MongoDB, Cassandra etc.
Load Balancing
Horizontal scaling allows MongoDB to balance the load.
MongoDB can handle multiple client’s requests to read and write the same data simultaneously by default. It uses some concurrency control mechanisms and locking protocols to ensure data consistency at all times.
High Availability
Auto Replication improves the availability of MongoDB database.
Indexing
Index is a single field within the document. Indexes are used to quickly locate data without having to search every document in a MongoDB database. This improves the performance of operations performed on the MongoDB database.
MongoDB Ubuntu 18.04 Set Up
Step 1: sudo apt update
sudo (Super User DO) command in Linux is generally used as a prefix of some command that only superuser are allowed to run. If you prefix “sudo” with any command, it will run that command with elevated privileges or in other words allow a user with proper permissions to execute a command as another user, such as the superuser. This is the equivalent of “run as administrator” option in Windows. The option of sudo lets us have multiple administrators
apt-get update downloads the package lists from the repositories and “updates” them to get information on the newest versions of packages and their dependencies.
Step 2: sudo apt install mongodb
Step 3: sudo systemctl status mongodb
What does systemctl command do: systemctl is used to examine and control the state of “systemd” system and service manager.
Step 4: Setting up user id and password
sub-step 1: list of all databases
$ mongo
> show dbs
sub-step 2:
> use admin
> db.createUser({user:"shivangi", pwd:"1234", roles:[{role:"root", db:"admin"}]})
Step 5: Edit service file:
$ sudo nano /lib/systemd/system/mongodb.service
Step 6
systemctl daemon-reload
sudo systemctl restart mongodb
sudo systemctl status mongodb
Step 7:
mongo -u "shivangi" -p --authenticationDatabase "admin"
How to Uninstall MongoDB
sudo apt-get purge mongodb mongodb-clients mongodb-server mongodb-dev
sudo apt-get purge mongodb-10gen
sudo apt-get autoremove####################################################################sudo apt list --installed | grep mongoAnswer:
mongo-tools/bionic,now 3.6.3-0ubuntu1 amd64 [installed,auto-removable]
mongodb-org/bionic,now 4.0.5 amd64 [installed]
mongodb-org-shell/bionic,now 4.0.5 amd64 [installed,automatic]
mongodb-server-core/bionic,now 1:3.6.3-0ubuntu1 amd64 [installed,auto-removable]
Solution:
sudo apt-get purge mongo-tools*
sudo apt-get purge mongodb*
How to visualise data inside MongoDB
Install MongoDB Compass
Download Compass
mongodb compass connect: https://docs.mongodb.com/compass/master/connect/
To download Compass, you can use your preferred web browser.
- Open the downloads page.
- Download the latest version of MongoDB Compass for Ubuntu. The MongoDB Compass installer is a
.deb
package.
Uninstall MongoDB Compass
delete mongodb compass: sudo dpkg — remove mongodb-compass