MongoDB — Introduction

Lokesh
3 min readJun 12, 2020

--

Why should we prefer Mongo DB instead of SQL Server?

SQL Server / Relational Database

  • Relational Database are organised by tables, the data is stored in the form of rows and columns.
  • Keys are used to tie the data between different tables.
  • Relational database can be relatively very complex for the simple schema like getting the info of single user we need to combine multiple tables as shown below,
Relational Database
  • Creating relational database schema can have several disadvantages, like the design can be complex, etc.
  • Changes to schema can require the changes for the entire database.
  • Complex queries to join the data.

Mongo DB / NO-SQL

  • Mongo DB is NO-SQL database which have the data in JSON format.But internally it is stored in BSON (Binary JSON).
  • A document related database contains all the required information in the same object, that makes the job easy for us.
  • Mongo DB are mostly used when the amount of data is really large and Relational database becomes slow.
  • In Mongo DB we have Collections — Tables(in relational database), Documents — Rows(in relational database) and Key Value pairs as in JSON object.
  • A JSON object is easy to understand.

Let’s get started with Mongo now,

Download and install the mongo DB community edition from — https://www.mongodb.com/

Once the setup is completed open CMD in C:\Program Files\MongoDB\Server\4.2\bin and in CMD enter the command

md \data\db

This command is used because mongo db needs a data directory to store all the data. By default mongo’s data directory path is the absolute path \data\db. so we create that directory to store the data.

  • Start Mongo db using command,
"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe"

This starts the main mongo db database process.

  • Now open another command prompt and connect the mongo db using the command,
"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"

Now we can enter the mongo db commands and play with it. Let’s see how,

  • To get the list of DB’s we can give the command,
show dbs
DB list
  • Now Say we want to use TMS DB we can give the command like use TMS, if the database name does not exists then it creates a new database with the name we give in use dbname command.
  • To see the collections in the database give show collections command.
collection list
  • Now we can see the logs collection with the command — db.logs.find({}) . This will find all the key value pairs in logs table.
Logs collection
  • To Insert a new log we can use — db.sample.insertOne({“Name”:”Lokesh”,”City”:”Chennai”}), This will insert a new records with the fields name and city in sample collection
Inserting a new record

By default the mongo db inserts a key, if it is not added by the user. It is unique for each entry.

  • To Update the city we can use the command — db.sample.updateOne({“Name”:”Lokesh”}, {$set:{“City”:”Mumbai”}}). This will update the city for name Lokesh as Mumbai
Update
  • To drop sample collection we can use — db.sample.drop().
Drop Collection

These are the basic operations with mongo, I will be posting more about Mongo in the upcoming stories!!

--

--