Saturday, June 19, 2021

MongoDB Tutorial For Beginners

mongodb,nosql,collection,bson,document,atlas
In this tutorial, we will discuss the following topics:

What is MongoDB

MongoDB is a document-oriented as well as a NoSQL database program. It can handle large volumes of data. As it is a NoSQL database, it stores data as documents, and the same type of documents are kept in the collection. Traditional relational databases use rows inside a table to store data but MongoDB uses documents, which is actually a JSON-like structure, to store data. 

The general structure of any MongoDB database is as follows:
NoSql,document-oriented database,document,MongoDB,collection,bson,

Documents in MongoDB

A record in traditional relational databases is a row inside a table. But MongoDB saves records as documents. The structure of a document is similar to a JSON object. We keep data in JSON in key (field) and value pair, as shown in the figure below. In documents, data is also kept in key (field) and value pair. When we view or update documents in Mongo Shell we are working with JSON.
NoSql,document-oriented database,document,MongoDB,collection,bson,

But there are some drawbacks of JSON, such as JSON being text-based and only supporting a limited data type. So MongoDB actually stores data in memory in BSON format. BSON stands for binary representation of JSON documents. The main advantage of BSON is it is fast and flexible. It also supports data types like date and binary data along with other data types like String, Number, Boolean, Array, etc. So with these data types, we can store and retrieve data from MongoDB with other programming languages.

Data in MongoDB is stored as BSON and viewed as JSON.

Connect to MongoDB

To play with MongoDB you need a MongoDB server like other relational databases. You can install a MongoDB server (mongo daemon) in your local machine or you can use MONGODB ATLAS which is basically a MongoDB server hosted in the cloud and you can easily connect and execute queries to the cloud DB from your local machine command prompt/terminal. I have installed MongoDB on my local Windows 10 machine.

Install MongoDB on Windows

To download the MongoDB community server, open this link - MongoDB Community Download. On the download page, you can download msi or zip file from ‘Available Downloads’ for Windows. I have downloaded the zip file. Actually, this zip contains all binary files that are required to run the MongoDB server.

Now unzip the zip file in the preferred location. Inside the folder, you can find the ‘bin’ folder and inside ‘bin’ folder you can find two .exe files: 1. mongod.exe, 2. mongo.exe.

mongod.exe and mongo.exe

Here, ‘mongod.exe’ is a Mongo Daemon that runs in the background and it will accept connection/request from MongoDB, it is a background process/service. And ‘mongo.exe’ is a command line shell that helps us to work with a database. So we need to run ‘mongod.exe’ first (in the background), then we run the mongo shell (mongo.exe) to interact with the MongoDB database.

We need to add the ‘bin’ folder path (the folder which contains ‘mongod.exe’ and ‘mongo.exe’) in the environment variable’s PATH variable so that we can run ‘mongod.exe’ and ‘mongo.exe’ from the command prompt/terminal.

Now create a folder named ‘data’ in the C drive on your local machine and inside the ‘data’ folder create another folder named ‘db’. This ‘db’ folder contains all database files. This step is very important, otherwise, the Mongo daemon will not start.

Now it is time to run the Mongo daemon. Open the command prompt and enter:
mongod
mongo daemon

To start Mongo shell you have to give the following command in the command prompt/terminal which will connect to a Mongo Daemon server that is running on localhost with default port 27017:
mongo
Now you have entered the Mongo shell:
MongoDB shell

So in this tutorial, we have learned what MongoDB is and how to start Mongo shell in a local machine.
in

How to create a database, collection, and document in MongoDB

No comments:

Post a Comment

Popular posts