Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

The Basics of MongoDB

Name: Raghavendra B M

https://www.drupal.org/u/raghavendra-b-m
https://www.linkedin.com/in/raghavendra-b-m-8b0923108/
Agenda
● Problems with RDBMS
● What is MongoDB
● Performance of SQL and MongoDB
● Comparison between SQL and MongoDB
● CURD operation in MongoDB
● Where clause commands
● Linking (Joins)
Why MongoDB why not MySQL?
● Before we understand what MongoDB is we need to
understand issues with traditional RDBMS.

ANY GUESS?
1. Scalability
❖ Difficult to scale millions of
millions of data.

❖ Data stored in multiple


tables (relationship) it is
difficult to scale.
2. Flexibility
❖ Fixed data structure therefore
not easy to make modifications
to data structure

❖ You need to spend hours and


hours on designing the
database before development

❖ In Agile projects database


requires constant restructuring
3. Performance
❖ Data is generally stored across
multiple tables. Joins have huge
performance impact as it
requires lot of CPU and
resources

❖ Need to install and configure


complex caching mechanism to
make it faster
MongoDB vs SQL performance chart
What is MongoDB?
❖ It is a NoSQL database called (Document database)

❖ It stores data in flexible JSON-like document.

➢ Easy to develop REST API in JSON

❖ It is highly scalable and flexible database


How MongoDB looks when compared
to RDBMS ?
[
{
first_name last_name email “first_name” : “Joe”,
“last_name” : “Satana”,
“email” : “[email protected]
Joe Satana [email protected] },
{
Bob Michel [email protected] “first_name” : “Bob”,
“last_name” : “Michel”,
“email” : “[email protected]
}
]
Comparison between SQL and MongoDB
SQL Server MongoDB
Database Database
Table Collection
Index Index
Row Document
Column Field
Joining Linking & Embedding
Where to use MongoDB?
● Big Data

● Content Management and Delivery

● Mobile and Social Infrastructure

● User Data Management

● Data Hub
MongoDB commands
mongo Enter the MongoDB client

show dbs List all database. Should have at least on record to display the
db in list.

db Display active database name

db.stats() Show the database name, number of collection and documents


in the database, etc.

use db_name To switch / create database

db.dropDatabase( Drop database


)
Collections = Tables in MongoDB is called as collections

Name Collection name


❖ To create a collection

➢ db.createCollection(name, Options ❖ capped - Overwrite oldest entries if


options) collection size is reached.
❖ autoindexId - Automatically index
➢ Eg : the _id field
db.createCollection(‘Employee ❖ size - Maximum size of the collection
s’) in bytes if capped = true
❖ max - Maximum number of
❖ Drop a collection documents allowed in collection

➢ db.collection_name.drop()
Where conditions
Operation Syntax Example RDBMS Equivalent

Equality {<key>:<value>} db.posts.find({"by":"tutorials point"}).pretty() where by = 'tutorials point'

Less Than {<key>:{$lt:<value>}} db.posts.find({"likes":{$lt:50}}).pretty() where likes < 50

Less Than {<key>:{$lte:<value>}} db.posts.find({"likes":{$lte:50}}).pretty() where likes <= 50


Equals

Greater Than {<key>:{$gt:<value>}} db.posts.find({"likes":{$gt:50}}).pretty() where likes > 50

Greater Than {<key>:{$gte:<value>}} db.posts.find({"likes":{$gte:50}}).pretty() where likes >= 50


Equals

Like {<key>:{'$regex':<value>}} db.posts.find({“title”: {'$regex': ‘How’} }) where title like ‘%How%’


Linking ( JOINS )
{
"name" : "Prashanth",

"mail" : "[email protected]",

"phone" : {
‘ph1’ : "8080808080",
‘ph2’ : "9090909090",
‘landline’ : "080-45454545"
}
}
Reference
● https://docs.mongodb.com/manual/reference

Question?
Thank you

You might also like