MongoDB - Getting Started

MongoDB

Next →
mongodb logo - tutorial - dyclassroom

This is the MongoDB getting started tutorial.

What is MongoDB?

MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. No database is more productive to use.
- mongodb.com

Prerequisite

If you have some prior knowledge of relational database (RDBMS like MySQL) then that is an advantage for you. Though MongoDB is not a RDBMS but having some database knowledge will help you understand MongoDB quickly.

Install MongoDB

Head over to the official website of MongoDB mongodb.com and download the latest version as per your operating system.

At the time of writing this tutorial MongoDB website provides a Community Server which you can download and install on your machine. Google MongoDB Community Server Download and you will get the download link.

They also provide MongoDB Atlas a global cloud database. For this you have to sign up and you will get a free cloud account.

For this tutorial we will be using the Community Server on our machine. Feel free to use Altas too.

Download the latest stable version preferably v4.0 or above.

Here is a link of the download center where you will find Community Server and Atlas.

https://www.mongodb.com/download-center

Please note the above download link may change in the future so Google search for the latest download link.

Assuming you have successfully downloaded and installed MongoDB on your computer lets go ahead and start using it.

Creating db directory

By default MongoDB will save its files inside /data/db directory. So, we have to open our Terminal and create this directory.

I am using a Mac so here is what I am using to create my directories. Windows will have a similar command to create directory/folder. Mac/Ubuntu share similar Terminal command so it should work fine.

$ sudo mkdir -p /data/db

Note! I have used sudo in the above command as only the root user has permission to create directory in that location.

On Windows you will need Administrative access. On Ubuntu sudo will do the work.

Change the owner of the db directory

Now change the ownership of the newly created db directory to your username so that you can start using it.

On Mac/Ubuntu you can run whoami command and it will tell you your username. Mine is yusufshakeel so I will use that.

$ cd /data
$ sudo chown -R yusufshakeel:yusufshakeel db

Start mongod

To know which version of mongod you are currently using type the following command in the Terminal.

$ mongod --version
db version v4.0.3

To run mongod run the following command in the Terminal.

$ mongod

This will start mongod and it will be running on port 27017.

Connecting to MongoDB server

Open a new Terminal and type the following command to connect to your local MongoDB server that you installed on your computer.

$ mongo

Exit from MongoDB server

To exit from MongoDB server type the following command.

> exit

Stop mongod

To stop mongod just hit Ctrl+C in the terminal where you started mongod.

Or check the Process ID of the mongod and kill it.

Example:

$ ps aux | grep mongod
yusufshakeel      2099   0.1  0.4  5101060  34572 s000  S+    7:09AM   0:02.39 mongod

On my computer I got the process id 2099 so to kill mongod I have to run the following command.

$ kill 2099

In the next tutorials we will learn about the terminology used in NoSQL database like MongoDB and how they are mapped with RDBMS databases like MySQL.

See you in the next tutorial. Please don't forget to share this tutorial link. Have fun developing :)

Next →