What is MongoDB?

Mongo DB is a NoSQL Database which is Not Only SQL Database.

It is an Open Source Database.

MongoDB is written primarily in C++

  • The core database engine (storage, indexing, replication, performance-critical parts) is implemented in C++ for speed and efficiency.

  MongoDB uses JavaScript as a scripting language, not as its foundation.

  JavaScript is used for:

  • The MongoDB Shell (mongosh)
  • Writing queries, aggregation expressions

MongoDB is used in Full Stack Web Applications:

2 Popular stacks :

MERNMEAN
MongoDBMongoDB
ExpressExpress
ReactAngular
Node.jsNode.js

NoSQL Databases :

Redis – Inmemory key value store

CouchDB

Cloud Firestore

MongoDB : Document Database

Relational Database :

Oracle

PostgreSQL

MySQL

NoSQL Databases :  Document Based, Dynamic Schema, Horizontally Scalable, Not as good

Relational Databases : Table Based, Predefined Schema, Vertically Scalable, Good for complex queries.

RDBMSMongoDB
DatabaseDatabase
TablesCollection
RowsDocuments
ColumnsFields
Table JoinEmbedded Documents
Primary KeyDefault_id provided by MongoDB

 MongoDB Structure :

  • Database :
    • A container for collections
    • Has own set of files on system
    • Can have multiple collections in single DB
  • Collections :
    • They are like tables in RDBMS
    • They contain a set of records, normally of similar type
  • Documents :
    • Set of key value pairs
    • Not required to have same set of fields.
    • Fields can have different types of data.
  • Default Port : 27017

Data types :

  • String
  • Integer
  • Boolean
  • Double
  • Arrays
  • TimeStamp
  • Date
  • Object (For embedded documents)
  • Binary Data
  • Plenty more

Single Document Inside Collection :

{                   “_id”                               :                   ObjectId(“632njd8430924hdghgu394”), ß ObjectID which is Hexadecimal String

                    “email”      :                   “info@infraxpertzz.com”, <- String

                    “status”     :                   “For your daily IT Activities” <- String

                    “tags”                             :                   [ “Oracle”, “MySQL”, “MongoDB”  ], <- Array of strings

                    “followers”                    :                   10, <- Integer

                    “comments”                 :                   [{ <- These are embedded Documents

                                                                                                    “username”                  :                   “abcd@gmail.com”,

                                                                                                    “text”                              :                   “Provide more videos”,

                                                                                                    “likes”                            :                   5

                                                                                                    “Number” :                   “123456789”

                                                                                  }

Unlike Relational Databases, you don’t need to create multiple tables to put details. Like if you need to add another phone number of an user in RDBMS, you need to add another table and then use primary or foreign keys. But in MongoDB, its not like that

“Number”                     :                   [ “123456789”, “789456123” ]

 So, you can see from above, we have an array to 2 phone numbers.

RDBMSMongoDB
Select * from infradb.infra.find()
Select * from infra
where status = “Published”
db.infra.find(
{ status: “Published” }
)
select * from infra
where status = “Published” or Topic = “MongoDB”
db.infra.find (
{ $or: [ {status: “Published” } , { Topic : MongoDB } ] }
)

MongoDB Data Format

JSON → JavaScript Object Notation

Data present in JSON is just a string

‘{

  “type”: “phone”,

  “price”: 120,

  “features”: [

    “HD Camera”,

    “WiFi”

  ]

}’

Difference b/w JSON & JavaScript Object

JSONJavaScript Object
JSON is a string formatJavaScript Object is a variable type
Keys must be in double quotesKeys need not be in quotes
Used for data storage & exchangeUsed for data manipulation in JS
Cannot be used directlyCan be used directly in JavaScript

Example Comparison

JSONJavaScript Object
‘{ “type”: “Phone”, “price”: 120, “inStock”: true, “features”: [ “HD Camera”, “Wi‑Fi”, “LTE” ]  }’{ type: “Phone”, price: 120, inStock: true, features: [ “HD Camera”, “Wi‑Fi”, “LTE” ] }

Conversion

OperationDescription
JSON.parse()Converts JSON → JavaScript Object
JSON.stringify()Converts JavaScript Object → JSON

JavaScript Object is a set of key‑value pairs surrounded by curly brackets.
It is not a string. It is an object in JavaScript.