0% found this document useful (0 votes)
28 views

Mongo DB Notes

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Mongo DB Notes

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Today's Agenda

---------------
Mongo DB
---------------
SQL :- Structured Query Language
Which has different commands to work with Structured Databases.

NoSQL :- UnStructured or Semi-Strucutured Databases.

What is a NoSQL?
It is a type of DBMS that is designed to handle and store large volumes of
unstrcutured or semi-structure data.

In General Traditional Databases uses tables and predefined schema to store data.
Whereas NoSQL uses for completely unstructred data.

EmpInfo is a Semi Strutural Data


Structural Data = Basic Emp Info
Un-Structural Data = Emp Documents/Images/Reports/

The NoSQL - Initially it was called as Non-SQL later it has been modified as "Not
Only SQL".

It has been classified into 4 types

1. Document Databases
These databases store data as semi-structured documents. Such as JSON
or XML.
It can queried using "Document Oriented Query Languages".
Ex:- MongoDB, CouchDB, Cloudant software comes under Document
Databases.
2. Key-Value Stores
These databases stores data in the form of Key-Value pairs(JSON).
Ex:- "Redis" is one of software comes under this section
3. Column-family Stores
These databases stores data as a column families whcih are set of
columns that are treated as a single entity
Ex:- Hbase, Big Table are some of the software comes under this
section
4. Graph Databases
These databases stores data as nodes and edges and are degined to
handle complex relationships between data.
Ex:- Amazon Neptune, Neo4j are the some of software comes under this
section.

When should NoSQL be used:-


1. When a huge amount of data needs to be stored and retrived.
2. The Data changes over time and is not structured.

In conclusion, NoSQL Databases offers several benefits over tridtional relational


databases such as
Flexibilty
Scalbility
Cost-Effectivness
-----
What is a Mongo DB?

It is one of the NoSQL Database. It is a Open - Source, Cross-platform and


document oriented database.
It provides high performance, high availability and automatic scaling.

What is the purpose of Developing MongoDB

All the modern applications required big data, fast features development, flexiable
deployments needed when compare to traditional database systems.

Features :-

Scalability
Performance
High Availability
Development faster
Deployment easier

Mongo DB uses JSON format to store data.

Key points to remembers

1. JSON Data Model with dynamic schemas.


2. Auto-Sharding for horizantal scalability
3. Built-in replication for high availability

When compare to RDBMS advantages of MongoDB:-

1. MongoDb is schema less :- Since it is not a having schema initally,


dynamically we can create and work with it whereas RDBMS is not.
2. There is no complex joins in MongoDB.
3. It provides the facility of the "deep query" because it supperts a powerful
dynamic query on documents.
4. It uses internal memory for storing working sets and this is the reason of its
fast access.
-----------------
Terminalogy of MongoDb

RDBMS MongoDB
-------------------------
Database Database
Table Collection
Row Document(BSON - Binary JSON)
Column Field
Index Index
Join Embeded Document
Forien Key Reference
Partition Shard
-------------------------
Data Types in MongoDB

String, Integer, Boolean, Double, Arrays, Object, Null, Symbol, Date


--------------------------
MongoDb Commands / Methods

First Open MongoDb Shell

Open Command Prompt, then type the following command


"mongosh"

How to create a database?


"USE" command is used to create / change from one db to another db.

show dbs :- this command shows all the databases which are already existed.
show collections :- this command shows all the collections created within the
database.

How to create a collection?


Method - 1 :- to create empty collection
db.createCollection("name")

How to add documents into the collection?


db.collectionName.insertOne(json object)
or
db.collectionName.insertMany(array of Json Objects)

Ex:-
db.collectionName.insertOne({"key":"val", "key":"val", .....})

db.Student.insertOne({"rollno":1001, "sname":"Pavan Kumar", "course":"java",


"fees":15000.00})

db.Student.insertMany(
[
{"rollno":1002, "sname":"Kiran Kumar", "course":"java", "fees":15000.00},
{"rollno":1003, "sname":"Kalyan Kumar", "course":"java", "fees":15000.00},
{"rollno":1004, "sname":"Suresh Kumar", "course":"java", "fees":15000.00}
])

Method - 2 :- creating collection with data directly.


db.NewCollectionName.insertOne/insertMany(jsonObject)

How to retrive documents ?


db.CollectionName.find() :- show all the documents
db.CollectionName.findOne() :- it shows only first document.
Quering Data
db.CollectionName.find({condition})

Ex:- Show all employees who are working as Tester


db.Employee.find({"job":"Tester"})

Ex:- Show all employees with only ename and job fields.
db.Employee.find({}, {ename:1, job:1}

Mongo Db tells the which column should include and which one should exclude.
include = 1
exclude = 0

Ex:- Show all Testers with only ename and job fields
db.Employee.find({"job":"Tester"}, {ename:1, job:1})
---
Mongo Db Query Operators

there are different type of operators


Comparison
$eq : equals
$ne : not equals
$gt : greater than(>)
$gte : greater than or equals(>=)
$lt : less than(<)
$lte : less than or equals(<=)
$in : matched within the array

Logical
$and
$or
$not
$nor

Evalution
$regex : Allows the use of regular expression when evaluting
field values
$text : Performs a text search
$where : uses a JavaScript expression to match documents

Ex:- Show all employee who are having 10000 salary.

db.Employee.find({salary:10000})
or
db.Employee.find({salary:{$eq:10000}})

Show all employees who are not having 10000 salary


db.Employee.find({salary:{$ne:10000}})

Show all employees who are having salary >12000


db.Employee.find({salary:{$gt:12000}})

Show all employees who are having salary >12000 include only ename,
salary fields
db.Employee.find({salary:{$gt:12000}}, {ename:1, salary:1})
----------------
db.Customers.insertMany(
[
{"cname":"Praveen", "location":"Hyderabad", "acc_banks":["SBI", "Axis"],
"cdate":Date()},
{"cname":"Pavan", "location":"Bangalore", "acc_banks":["SBI", "Axis", "HDFC"],
"cdate":Date()},
{"cname":"Paramesh", "location":"Kolkatta", "acc_banks":["HDFC", "Axis"],
"cdate":Date()},
])

Show all customers who are having account in HDFC bank

db.Customers.find({acc_banks:{$eq:"HDFC"}})
or
db.Customers.find({acc_banks:{$in:["HDFC"]}})

Show all customers who are having accounts either in SBI or HDFC

db.Customers.find({acc_banks:{$in:["HDFC", "SBI"]}})
db.EmpInfo.find({JOB:{$in:["Developer","Tester"]}})
Examples on Logical Operators

$and

Show all emp whose salary >10000 and <15000


db.Employee.find({$and:[{salary:{$gt:10000}}, {salary:{$lt:15000}}]})
$or

show all emps who are working as either developer or testers

db.Employee.find({$or:[{job:{$eq:"Developer"}}, {job:{$eq:"Tester"}}]})

$nor

Show all employees who are not Testers

db.Employee.find({$nor:[{job:{$eq:"Tester"}}]})

You might also like