MongoDB is a popular database program. This is highly popular for cross-platform development.

It doesn’t have any tables but stores data in a JSON like file system. Entries are called documents.

Speciality about MongoDB is: in MongoDB you don’t need to create predefined table structures. You can add any kind of data into a db. No need to add format during creating the table.

Some of its characteristics are:

  • Opensource
  • Document-based DB
  • Schemaless
  • Cross-platform

Advantages of MongoDB over Relational Database Management System (RDBMS)

  • Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.
  • Structure of a single object is clear.
  • No complex joins.
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that’s nearly as powerful as SQL.
  • Tuning.
  • Ease of scale-out − MongoDB is easy to scale.
  • Conversion/mapping of application objects to database objects not needed.
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data.

Why Use MongoDB?

  • Document Oriented Storage − Data is stored in the form of JSON style documents.
  • Index on any attribute
  • Replication and high availability
  • Auto-Sharding
  • Rich queries
  • Fast in-place updates
  • Professional support by MongoDB

Where to Use MongoDB?

  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub

Installation and Hands-on

MongoDB is pretty easy to install and use. You can download and use it just like you do MySQL. Although we just easily install LAMP / XAMP or similar alternatives more for MYSQL. For MongoDB, you can also use a cloud version of it. We will see how to use it in a Windows-based system. Other than the installation, everything else is the same for cloud, windows or Linux versions.

C:\Program Files\MongoDB\Server\4.2\bin\mongo.exe

1. Start with mongo then show dbs

show dbs

2. To perform CRUD operation, at first we will need to create a databasein MongoDB to start with. Let’s create a db named demo. We will use the following command:
use ‘db_name’ => use demo

use demo

3. Show collections will list all collections inside the db

show collections

4. Now we will add a new document inside the db we created. We insert”one” or insert ”many” or multiple documents together. Let’s add multiple lines

{ name: "rinas", age: 45, measurement: { h: 10, w: 15.25 }, grade: "A" });

Inserting multiple documents (entries) at once:

db.demo.insertMany( [
{ name: "sumon", age: 25, measurement: { h: 14, w: 21 }, grade: "A" },
{ name: "shujon", age: 50, measurement: { h: 8.5, w: 11 }, grade: "A" },
{ name: "shahin", age: 100, measurement: { h: 8.5, w: 11 }, grade: "D" },
{ name: "shofik", age: 75, measurement: { h: 22.85, w: 30 }, grade: "D" },
{ name: "rina", age: 45, measurement: { h: 10, w: 15.25 }, grade: "A" }

5. Now we can read the data from the database

db.inventory.find( { “name”: "rina"})

db.inventory.find().limit(2) //show first two data from inventory collection

6. Similar to insert one and insert many we can update one or update many. For example:

db.demo.updateOne({ name: "sumon"},{\$set: { name: "pencil", age: 35, measurement: { h: 20, w: 25 }, grade: "E" }})

7. We can also do replace operation. We are replacing the document “shofik” with “safa”.

db.demo.replaceOne({ name: "shofik"},{ name: "safa", age: 25, measurement: { h: 30, w: 35 }, grade: "F" })

8. Similar to insert one and insert many we can delete one and delete many. For example we are deleting one document where name is equal to “rina” with the following code:

db.demo.deleteOne({ name: "rina"})

MongoDB is pretty popular these days as we have discussed already. We will see some more application of it in next posts.