Currently, I am starting my journey to expand my knowledge in MongoDB, and I have decided to create this series here and share all the knowledge that I get, with the community.
Table of Content:
MongoDB is a NoSQL database. It is different from traditional SQL based databases. Why different ? First, it doesn't have a predefined structure like SQL tables. We don't need to define how our table will look like before hand. Second, it doesn't use tables, instead it uses JSON-like documents and it's up to you to choose if you want a schema or not.
RDBMS: Relational Databases
NoSQL : Not SQL or Non Relational Databases or Distributed Databases
Benefits of NoSQL:
- Dynamic schema for data that is unstructured. Each document can have unique structure.
- It is optional to define Schema.
- NoSQL databases are are horizontally scalable
- Best suited for storing hierarchical data.
I am using the following OS and mongodb version. It will not affect the learning process that much but good to know before continuing.
- Operating System: Ubuntu 18.04 LTS
- Mongodb Version: 4.2.9
Installation is quite easy and you can follow mongodb docs and install the community edition Here. If during installation it prompts you to install mongodb compass then install it. It is a GUI application that will help you visualize your databases in an intuitive manner.
The two important things that you will get from installation are:
- mongod : the mongodb server
- mongo : the mongo shell, in which we can issue commands and play with the db
In linux, starting mongodb is as easy as typing a few commands in terminal. So let's do it
Starting mongod server
# start server
sudo systemctl start mongod
# start server : alternative way
sudo service mongod start
# check if server running
sudo systemctl status mongod
# check if server running : alternative way
sudo service mongod status
# stop server
sudo systemctl stop mongod
# stop server: alternative way
sudo service mongod stop
Once server is started, we can connect mongo shell with the running instance. To access mongo shell, use the following command:
Yes, it is as easy as typing
mongo and pressing Enter.
Hopefully you will be in shell. There will be some warnings and other stuff. To clear the terminal, either type
cls in shell and press Enter, or press
ctrl + L.
To exit out of the shell, type
exit and press Enter.
Before moving forward, let's discuss some important things that we need to know to understand MongoDB in a better way.
In mongodb you can have multiple databases on a single mongodb server to hold data in a collective manner. E.g. If you are working on a school application, you would create a school database in which all your collections(similar to table in sql) reside. This database can have collection of students, collection of teachers, etc.
A useful command to know when working with databases is:
> show dbs
This command will show you all the available databases that you have on your mongo server. And to switch between databases, you can simply type:
> use <database name>
A collections is like a table in SQL. It holds your record in one place. E.g. You can create a collection of students, in which you can store records of all the students. In these records, you can have student's name, age, class, subjects, percentage etc.
A collection holds multiple
documents in one place.
We will discuss more about collections in further posts.
Some useful commands to know for collections are: (switch to a database with
use <database name> to use these commands)
# show all the collections in db
> show collections
# using a collections
> db.<collection name>.find()
Documents are the main records that hold your information in JSON-like format.
"name": "Ash Ketchum",
"subjects": ["Math", "Science", "History", "English"],
One thing to keep in mind is that the structure of document is not rigid, that is, it is not necessary for us to define all the fields in all documents. This makes mongodb flexible for unstructured data.
We will learn more about documents in further posts.
So that's it for this one. In the next post, basic CRUD operations will be covered.
Any Suggestions are much appreciated.
Hope you find it useful and learned something new from it.
Happy Coding :)
Next Post : Basic CRUD Operations