Learn MongoDB: Getting Started
Tags: mongodb, tutorial, webdev, beginners
Published: 2020-10-03 14:04:00

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.

Let's Begin..

Table of Content:
- [What is MongoDB](#what-is-mongodb-)
- [Things to know](#things-to-know)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Basics](#basics)

### What is MongoDB ?

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 
- It uses objects with key-value pairs. So, if you are familiar with JSON or javascript objects, it will be easier to understand and use NoSQL for you.
- Best suited for storing hierarchical data. 



## Things to know

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

Installation is quite easy and you can follow mongodb docs and install the community edition [Here](https://docs.mongodb.com/manual/administration/install-community/). 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:

1. mongod : the mongodb server 
2. mongo : the mongo shell, in which we can issue commands and play with the db



## Getting started

In linux, starting mongodb is as easy as typing a few commands in terminal. So let's do it 

**Starting mongod server**

```bash
# 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:

```bash
mongo
```



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. 



## Basics

Before moving forward, let's discuss some important things that we need to know to understand MongoDB in a better way.

#### Database

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:

```bash
> 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:

```bash
> use <database name>
```



#### Collections

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)

```bash
# show all the collections in db
> show collections

# using a collections
> db.<collection name>.find()
```



#### Documents

Documents are the main records that hold your information in JSON-like format. 

```json
{
   "name": "Ash Ketchum",
   "age": 20,
   "subjects": ["Math", "Science", "History", "English"],
   "standard": 8
}
```



Creating a document is as easy as creating an object in javascript. You can nest values. Have document inside documents. You can have arrays, objects, boolean values, numbers, strings etc. This makes mongodb good for hierarchical data where nesting is required.

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](https://dev.to/paras594/learn-mongo-crud-basics-5bpk)