Latest Code Tutorials

NoSQL MongoDB Tutorial With Example From Scratch

NoSQL MongoDB Tutorial With Example From Scratch is today’s topic. MongoDB is an open-source document database and leading NoSQL database. MongoDB is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document. It is not Relational Database like MySQL or Oracle. In MySQL database, there is Database then Tables and then Columns. In MongoDB structure is like Database then Collections and then Documents. So there is no concept like Tables and Columns in the MongoDBMongoDB database is vastly popular with Node.js platform. 

If you want to learn more about Node.js then check out this course NodeJS – The Complete Guide (incl. MVC, REST APIs, GraphQL)

NoSQL MongoDB Tutorial

First, we learn the basic structure of MongoDB database like what is collections and what is a document. Then we will install MongoDB on Mac System and then play with it.


A collection is a group of MongoDB documents. It is the equivalent of a Relational Database table. A collection exists within a single database.


The document is a set of key-value pairs. Documents have a dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and standard fields in a collection’s documents may hold different types of data.

Example of Document

	"_id" : ObjectId("5ac0ac19d412d208b8c91214"),
	"course_name" : "react.js",
	"course_price" : 10,
	"__v" : 0

It is like JSON Object. The document contains key-value pairs. Here, _id is generated by MongoDB. It is equivalent to id in MySQL database; the values are different because in most case in MySQL id is incremental field starting from 1.

Install MongoDB on Mac.

If you have installed MongoDB database on Mac, then skip this step. For newbies, go to your terminal and hit the following command to install homebrew. If you have installed, then do not type this command.

/usr/bin/ruby -e "$(curl -fsSL"

Next, add Homebrew’s location to your $PATH  in your .bash_profile or .zshrc file.

export PATH="/usr/local/bin:$PATH"

Now, install MongoDB using Homebrew.

Type the following command in your terminal.

brew install mongodb

After downloading Mongo, create the “db” directory. This is where the Mongo data files will live. You can create the directory in the default location by running mkdir -p /data/db

Make sure that the /data/db directory has the right permissions by running the following command.

sudo chown -R `id -un` /data/db

Run the Mongo daemon, in one of your terminal windows run mongod. It should start the Mongo server at port number 27017.


Install MongoDB on Mac

Now, your MongoDB server is ready to connect any web application. Once you are connected, you can see here that one connection is added. While you are working with Any web project that needs to use MongoDB database, you need to open this connection, do not ever close terminal. Otherwise, the connection between a web application and database will be lost. So never down mongo server.

MongoShell Interface

mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. 

Now, if we want to see how many databases are there by default, then we can use the following command.

show dbs

MongoDB Database Example

Create Database.

Now, we can create one database using the following command.

use demodb

If the database is not there, then it will create for us. If already there, then it switches to that database.

To check your currently selected database, use the following command.


MongoDB Tutorial

Create Collection.

The cool thing about MongoDB is that you need not create a collection before you insert the document into it. With a single command, you can add a document in the collection, and the MongoDB creates that collection on the fly.

db.person.insert({name: 'mahavir', age: 70})

It will create a collection called person and add one document in it.

So, you can see the inserted documents using the following command.


For a prettier result, we can query like this.


Beginners guide to up and running with MongoDB

Shell Help

To see the list of help, in the mongo shell type the following.


It will list the commands. You can use it to find help in database administration, query the results and other stuff.

mongodb help

Now, let us say, we need help to find all the documents in the person collection then we can go through help to execute the right query.

From starting, first, check whether you are in the right database or not. So first switch to a right database.

use demodb

Then, check whether you are in the collection or not.


If not then switch to the right collection using the following command.

use person

Now, type the following code to get all the methods that can apply to any collection.

Database collection help

Now, you can apply the first command that will return all the documents. But you can go further for help. Type the following command.


So, you will see what other methods you can chain with find() method to get the desired result.

MongoDB NoSQL Database Tutorial

You can also drop the database using the following command.


We will deep dive into NoSQL MongoDB database in future posts. That is it, for now, NoSQL MongoDB Tutorial is over. Thanks for taking.

  1. Josué Mora says

    Thank you, this website is great. I am learning how to connect React with Backend and your last tutorial about it was very powerful.

  2. arman says

    Great Tutorial!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.