DEV Community

Cover image for Learn MongoDB: Getting Started
Paras 🧙‍♂️
Paras 🧙‍♂️

Posted on • Updated on

Learn MongoDB: Getting Started

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 ?

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. 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

# 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
Enter fullscreen mode Exit fullscreen mode

Once server is started, we can connect mongo shell with the running instance. To access mongo shell, use the following command:

mongo
Enter fullscreen mode Exit fullscreen mode

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:

> show dbs
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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)

# show all the collections in db
> show collections

# using a collections
> db.<collection name>.find()
Enter fullscreen mode Exit fullscreen mode

Documents

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

{
   "name": "Ash Ketchum",
   "age": 20,
   "subjects": ["Math", "Science", "History", "English"],
   "standard": 8
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (6)

Collapse
 
sabarishcodes profile image
Sabarish Rajamohan

Good introduction to MongoDB. Thanks for sharing Paras. Would be following the series that us get to come :)

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Glad to know that :)

Collapse
 
kulu123z profile image
KULDIP MOCHI

Useful thanks, keep writing more

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Welcome :)

Collapse
 
manchestergriff profile image
ManchesterNews.com

thank you very much

Collapse
 
paras594 profile image
Paras 🧙‍♂️

Happy to know it helped you !!...stay tuned for more advanced stuff :)