Mongo DB Notes
Mongo DB Notes
---------------
Mongo DB
---------------
SQL :- Structured Query Language
Which has different commands to work with Structured 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.
The NoSQL - Initially it was called as Non-SQL later it has been modified as "Not
Only SQL".
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.
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
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
show dbs :- this command shows all the databases which are already existed.
show collections :- this command shows all the collections created within the
database.
Ex:-
db.collectionName.insertOne({"key":"val", "key":"val", .....})
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}
])
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
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
db.Employee.find({salary:10000})
or
db.Employee.find({salary:{$eq:10000}})
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()},
])
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
db.Employee.find({$or:[{job:{$eq:"Developer"}}, {job:{$eq:"Tester"}}]})
$nor
db.Employee.find({$nor:[{job:{$eq:"Tester"}}]})