0% found this document useful (0 votes)
68 views29 pages

Basics of NoSQL, Mongo DB

Uploaded by

palamakularamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views29 pages

Basics of NoSQL, Mongo DB

Uploaded by

palamakularamesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Basics of NoSQL Databases -

MongoDB
As per CBSE curriculum
Class 11

Chapter- 20
By-
Neha Tyagi
PGT (CS)
KV 5 Jaipur(II Shift)
Jaipur Region
Introduction
• Till now we have been working on the databases
which were based on SQL consisting of table,
row, fields ,records etc.

• It is possibe to have database without any


structure or record. NoSQL or Not Only SQL
Databases are such databases.

• We will learn NoSQL databases in this chapter.

Neha Tyagi, KV5Jaipur II Shift


NoSQL Databases
• These are non-relational databases which does not have any
strict or rigid structure.
• These does not store records on the basis of conventional
tables.
• These runs in clusters and stores data on the basis of web.
These are high in scalability. These are also known as
bigdata.
• You have worked on several apps/web apps using such
databases like Google Mail, Google Earth, Ebay, LinkedIn,
facebook, Amazon etc.
• These provides fast response time.
• These can handle data of any kind without any restriction.
• These adopts new features and fast update.
• These does not show down time.
Neha Tyagi, KV5Jaipur II Shift
Types of NoSQL Databases
1. Key-value Databases
2. Document Databases
3. Column family stores Databases
4. Graph Databases

Key-Value databases
• Just like python dictionary.
• Very simple and flexible.
• Examples-Cassandra, Amazon DyanmoDB,
ATS (Azure Table Storage, Riak, BerkeleyDB

Neha Tyagi, KV5Jaipur II Shift


Document Databases
• These are advanced form of key-value
databases.
• Here, key-value pair stores in document in
structured or semi-structured form.
• Keys are always of string type, values can
be of any type.
• It can be in the form of MS office
document, PDFs, XML, JSON ,BSON.
• JSON (JavaScript Object Notation) and
BSON (Binary JSON)
• JSON is an open, human & machine
understandable standard. Its main format
to interchange data on modern web is
XML.
• We have learnt use of JSON in Python
dictionaries.
• Its examples are - MongoDB, Couch DB
DocumentDB etc.
Neha Tyagi, KV5Jaipur II Shift
Column Family Store Database
• These are known as column store
or column family databases and
these are column oriented
models.
• Column family is a storage
mechanism which can –
– Have multiple rows.
– Each row can have multiple
columns.
– In this, there is a row key under
which there can be multiple
columns as shown in the figure.
– Its examples are- Hbase,
Cassandra, HyperTable etc.
Neha Tyagi, KV5Jaipur II Shift
Graph Database
• It uses graphical model to store
data.
• Here, nodes are used to show
object whereas edges are used
to show relation between those
nodes.
• Its examples are- Neo4j,
Blazegraph, Titan etc.

Neha Tyagi, KV5Jaipur II Shift


Advantages and Disadvantages of NoSQL Databases
•Advantages: •Disadvantages:
–Flexible Data Model –Lack of Standardization
These are very flexible database No standard rules are there for
which can store any kind of data. NoSQL database.
–Evolving Data Model –Backup of Database
You can change its schema without Main problem with NoSQL
downing the system. databases is of backup. MongoDB
–Elastic Scalability provides tool for backup but it is also
Huge database can be stored on a not up to the mark.
very less cost. –Consistency
–High Performance NoSQL database does not think
Time of throughput and latency is about consistency. Means here, you
very less. can have duplicate data very easily.
–Open Source
It is available free of cost and you
can change it as per yor
requirement.

Neha Tyagi, KV5Jaipur II Shift


Working with MongoDB
•MongoDB is a document-oriented NoSQL database.
•It supports dynamic schemas which shws data in JSON format.
•It is a free open source software which gives scalability and high
performance.

MongoDB Terminology
MongoDB Term Description SQL Term
Field a name-value pair which stores information. Column
Document Group of Locally related fields. Row/record
Collection Group of Related documents. Table
Database A container for Collections. A MongoDB server can Database
have multiple databses.
Primary key Unique field identifies document. Primary key

Neha Tyagi, KV5Jaipur II Shift


Installing MongoDB
•Copy the following link and paste in browser.
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/#install-mdb-edition

Download msi
version from
here

Neha Tyagi, KV5Jaipur II Shift


Installation of MongoDB
•Install MongoDB by opeing MSI file.
•After installation, check the availability of mongodb.exe
file and mongo.exe file using following path-
C:\Program Files\MongoDB\Server\4.0\bin
•After this, create a data folder on c:\ and db folder under
data folder i.e. “c:\data\db”.
•Now, run mongodb by using the location C:\Program
Files\MongoDB\Server\4.0\bin from command prompt.
Do not close mongodb after run.
•Now, in other command window run mongo using the
same path.
Neha Tyagi, KV5Jaipur II Shift
Starting MongoDB
This is mongodb’s installer which is of about 190 MB.
In my computer, it has been installed on “C:\Program Files\MongoDB\Server\4.0\bin” path.
You cancheck its path in your computer. We can add it in window’s path.

It is required to have mogodb in running state before running Mongo. Now, you
are ready to give commmand on mongo.

Neha Tyagi, KV5Jaipur II Shift


MongoDB Data Types
S.N. Data Type DataType S.N. Data Type DataType
Number Number

1. Double 1 10. Null 11


2. String 2 11. Regular Expression 12
3. Object 3 12. JavaScript 13
4. Array 4 13. Symbol 14
5. Binary Data 5 14. JavaScript with scope 15
6. Undefined 6 15. Integer 16 and 18
7. Object Id 7 16. Timestamp 10
8. Boolean 9 17. Min Key 255
9. Date 10 18. Max Key 127

Neha Tyagi, KV5Jaipur II Shift


MongoDB - Basic commands
•Creation of Database 
It is not required to create seperate database in MongoDB. As soon as you
insert first information n database, database automaticaly created.
•Displaying Current Database
>show dbs it shows database
>show collections it shows collections of current database
•Using Database
>use mydb
•CRUD operations 
The operations are as under -
Create
These are knows
Read
as CRUD
Update operation
Delete

Neha Tyagi, KV5Jaipur II Shift


MongoDB - Basic commands
•Creation of databse using Save operation 
It is not required to create seperate database in MongoDB. As soon as you
insert first information n database, database automaticaly created.
– You can input data in collection by save or insert command-
db.<collection-name>.save({<document details>})
– We can use show collections command to confirm creation of collection.
– >USE <DatabaseName> can also be used to create database
– Following example shows creation of school database and input of 1
collection.

Neha Tyagi, KV5Jaipur II Shift


MongoDB - Basic commands
•Creation of databse using Save operation
– We can insert multiple document like --

Here, 2 documents
are inserted.

– When you insert a document, mongoDB adds a field itself


“_id” it sets its value in increasing order. This process is not
visible to us. If we desire, we can give value of “_id” at the
time of insertion.
•If you insert a document using Save or insert and name is not
received from given database or collection then mongoDB creates
a new database for it.
Neha Tyagi, KV5Jaipur II Shift
MongoDB -Basic commands
•Creation of database from Insert operation
– You can insert data in collection using insert command-
db.<collection-name>.insert({<document details>})
– We can use show collections command to confirm creation of collection.
– Following example shows creation of school database and input of 1
collection.

– We can insert multiple document like


– >db.teachers.insertMany([{name:’Ratan’},{name:’Krishna’,age:45}])

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic commands
•Document can also be inserted by object creation.

Here stud is a valid mongoDB
object.

•An object can have a field which is an object itself.

address is a field of stud , its


value is addr which is an object.

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic commands
•An Object can have arrays too. For ex –
Name: Himanshu
Here subject is an array
Class:11
Section: A
Subjects: English, Hindi, Maths, Physics, Chemistry

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic commands
•Read Operation:
Read operation is used access documents from collection of database.
Syntax is-
>db.<collection-name>.find() will show all documents of collection.
>db.<collection-name>.findOne() will show only one record.
>db.<collection-name>.findOne({<key>:<value>}) it will work like search
criteria.
If no record matches then it
returns null.

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic commands
•Read Operation:

pretty( ) prints documents in


JSON format with proper
indentation.

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic commands
•Read Operation:

With the above given example only name field will be displayed with “_id”.

If you don’t want to display “_id” then command will be like-

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic Operators
•Comparison Operator:
Like other databases, mongoDB also provides operators so that
we can perform delete, read or update operations.
Operator Meaning
Name

$eq Equal to

$gt Greater than

$gte Greater than or


equal to

$lt Less than

$lte Less than or


equal to

$ne Not equal to

Neha Tyagi, KV5Jaipur II Shift


MongoDB-Basic Operators
•Comparision Operator:
Conditional base or range is to be given as-
{field:{$gte:<lower value>, $lte:<upper value>}}

Neha Tyagi, KV5Jaipur II Shift


MongoDB -Basic Operators
•Condition based on List/Array Operator Name Meaning
{ field :{ $in : [ val1,val2, . . . . . ] } } $in In
{ field :{ $nin : [ val1,val2, . . . . . ] } } $nin Not In

Data shown on Section matching


but no data shown on no match.

Neha Tyagi, KV5Jaipur II Shift


MongoDB-Basic Operators
•Logical Query Operators Operator
Name
Meaning

{ field :{ $not :{<op-Exp>}} }


$not Logical
{ field :{ $and :[{<op-Exp>}, {<op-Exp>},..]} }
NOT
{ field :{ $or :[{<op-Exp>}, {<op-Exp>},..]} }
$and Logical
AND
$or Logical
OR

Neha Tyagi, KV5Jaipur II Shift


•Update Operation:
Update operation can be used in two ways-
>update/updateOne or >updateMany ($set operator is used with it)
>db.<CollectionName>.update/updateOne({query-exp},{$set:{<field1>:<val1>, . . . }}

Only to make it understand this example


has taken otherwise updation always to
be made with primary key. Here name
has modified as Hari Prakash where age
was 12.

If you need to update multiple matching


records then you should use
updateMany( ).

Neha Tyagi, KV5Jaipur II Shift


•Delete Operation:
Delete operation can be used in two ways-
>deleteOne or >deleteMany
>db.<CollectionName>.deleteOne({<filter Exp>}) it will delete only one record
even on multiple matching.
>db.<CollectionName>.deleteMany({<filter Exp>}) it will delete multiple records
on multiple matching.

“Hari Prakash”
record is deleted.

Neha Tyagi, KV5Jaipur II Shift


Thank you
Please follow us on our blog-

www.pythontrends.wordpress.com

Neha Tyagi, KV 5 Jaipur

You might also like