Sanjay Mate
Asst. professor
IICMR - MCA
Introduction to NoSQL database
Advantages of NoSQL database
SQL Vs NoSQL
Introduction to MongoDB with python
Exploring Collections and Documents
Performing basic CRUD operations with MongoDB and python
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
2
NoSQL databases (aka "not only SQL") are non tabular, and store
data differently than relational tables.
They provide flexible schemas and scale easily with large
amounts of data and high user loads.
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
3
SQL is a standard database language used to access
and manipulate data in databases. SQL stands for
Structured Query Language. SQL was developed by
IBM Computer Scientists in the 1970s. By executing
queries SQL can create, update, delete, and retrieve
data in databases like MySQL, Oracle, PostgreSQL,
etc. Overall SQL is a query language that
communicates with databases
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
4
To support client/server architecture, software engineers use SQL
to establish the connection between back-end and front-end.
SQL can also be used in the 3-tier architecture of a client, an
application server, and a database.
SQL is used as a Data Definition Language(DDL) in which we can
independently create a database, define the structure, use it, and
discard it when its work is done.
SQL is used as a Data Manipulation Language(DML) in which we
can enter data, modify data, extracting data.
SQL is used as a Data Control Language(DCL) it specifies how we
can protect our database against corruption and misuse.
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
5
NoSQL databases emerged in the late 2000s as the cost of storage
dramatically decreased. Gone were the days of needing to create a
complex, difficult-to-manage data model in order to avoid data
duplication. Developers (rather than storage) were becoming the
primary cost of software development, so NoSQL databases optimized
for developer productivity
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
6
data came in all shapes and sizes — structured, semi-
structured, and polymorphic — and defining the schema in advance
became nearly impossible. NoSQL databases allow developers to store
huge amounts of unstructured data, giving them a lot of flexibility.
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
7
the Agile was rising in popularity,
Cloud computing also rose in popularity
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
8
Document Databases
Key-value Databases
Wide-column stores
Graph Databases
Python Programming- by Mr. Sanjay Mate , IICMR-MCA
9
Document Databases
A document database stores data in JSON, BSON, or XML documents (not
Word documents or Google Docs, of course). In a document database,
documents can be nested. Particular elements can be indexed for faster
querying.
Use cases include ecommerce platforms, trading platforms, and mobile
app development across industries.
Python Programming By Sanjay Mate, IICMR-MCA
10
Document Databases
JSON Object
Python Programming By Sanjay Mate, IICMR-MCA
11
Document Databases
Python Programming By Sanjay Mate, IICMR-MCA
12
Key-value Databases
Every data element in the database is stored as a key value pair consisting of an
attribute name (or "key") and a value.
Use cases include shopping carts, user preferences, and user profiles
Python Programming By Sanjay Mate, IICMR-MCA
13
Key-value Databases
Python Programming By Sanjay Mate, IICMR-MCA
14
Wide-column stores
While a relational database stores data in rows and reads data row by row, a
column store is organized as a set of columns. This means that when you want to
run analytics on a small number of columns, you can read those columns directly
without consuming memory with the unwanted data
Columnar databases can quickly aggregate the value of a given column
(adding up the total sales for the year, for example). Use cases include
analytics..
Python Programming By Sanjay Mate, IICMR-MCA
15
Wide-column stores
Python Programming By Sanjay Mate, IICMR-MCA
16
Graph databases
Store data in nodes and edges. Nodes typically store information about
people, places, and things, while edges store information about the
relationships between the nodes.
Think about real-time recommendations on an e-commerce site, where the
application needs to connect data about what the user is looking for, what the
user has bought in the past, what users like this user have bought, what
preferences and interests the user has, what products go well with the product
being viewed, what is currently in stock, and more.
Python Programming By Sanjay Mate, IICMR-MCA
17
Graph databases
Python Programming By Sanjay Mate, IICMR-MCA
18
Let's consider an example of storing information about a user
and their hobbies. We need to store a user's first name, last
name, cell phone number, city, and hobbies.
Python Programming By Sanjay Mate, IICMR-MCA
19
In order to retrieve all of the information about a user and their
hobbies, information from the Users table and Hobbies table
will need to be joined together.
Python Programming By Sanjay Mate, IICMR-MCA
20
Let's consider how to store the same information about a user
and their hobbies in a document database like MongoDB..
In order to retrieve all of the information about a user and their hobbies,
a single document can be retrieved from the database. No joins are
required, resulting in faster queries.
Python Programming By Sanjay Mate, IICMR-MCA
21
References
Python Programming By Sanjay Mate, IICMR-MCA
22
SQL Databases NoSQL Databases
• Document: JSON documents
• Key-value: key-value pairs
Data Storage Tables with fixed rows
• Wide-column: tables with rows and dynamic
Model and columns
columns
• Graph: nodes and edges
Developed in the
Developed in the late 2000s with a focus on
Development 1970s with a focus on
scaling and allowing for rapid application change
History reducing data
driven by agile and DevOps practices.
duplication
• Document: MongoDB and CouchDB
Oracle, MySQL,
• Key-value: Redis and DynamoDB
Examples Microsoft SQL Server,
• Wide-column: Cassandra and Hbase
and PostgreSQL
• Graph: Neo4j and Amazon Neptune
Python Programming By Sanjay Mate, IICMR-MCA
23
SQL Databases NoSQL Databases
• Document: general purpose
• Key-value: large amounts of data with simple
lookup queries
Primary
General purpose • Wide-column: large amounts of data with
Purpose
predictable query patterns
• Graph: analyzing and traversing relationships
between connected data
Schemas Rigid Flexible
Vertical (scale-up
Scaling Horizontal (scale-out across commodity servers)
with a larger server)
Python Programming By Sanjay Mate, IICMR-MCA
24
SQL Databases NoSQL Databases
Multi-Record Most of the NOSQL DBs , do not support multi-
ACID Supported record ACID transactions.
Transactions However, some—like MongoDB—do.
Joins Typically required Typically not required
Many do not require ORMs.
Requires ORM
Data to Object MongoDB documents map directly to data
(object-relational
Mapping structures in most popular programming
mapping)
languages.
Python Programming By Sanjay Mate, IICMR-MCA
25
Flexible data models
Horizontal scaling
Fast queries
Easy for developers
Python Programming By Sanjay Mate, IICMR-MCA
26
Don’t support ACID (atomicity, consistency, isolation, durability) transactions
across multiple documents. With appropriate schema design, single record
atomicity is acceptable for lots of applications. However, there are still many
applications that require ACID across multiple records.
NoSQL databases can be larger than SQL databases – data models in NoSQL
databases are typically optimized for queries and not for reducing data
duplication,. Storage is currently so cheap that most consider this a minor
drawback, and some NoSQL databases also support compression to reduce the
storage footprint.
Depending on the NoSQL database type you select, you may not be able to
achieve all of your use cases in a single database.
Python Programming By Sanjay Mate, IICMR-MCA
27
One of the most frequently cited drawbacks of NoSQL databases is that
they don’t support ACID (atomicity, consistency, isolation, durability)
transactions across multiple documents. With appropriate schema design,
single record atomicity is acceptable for lots of applications. However,
there are still many applications that require ACID across multiple records.
To address these use cases MongoDB added support for multi-document
ACID transactions in the 4.0 release, and extended them in 4.2 to span
sharded clusters.
Python Programming By Sanjay Mate, IICMR-MCA
28
Since data models in NoSQL databases are typically optimized for queries and not
for reducing data duplication, NoSQL databases can be larger than SQL databases.
Storage is currently so cheap that most consider this a minor drawback, and some
NoSQL databases also support compression to reduce the storage footprint.
Depending on the NoSQL database type you select, you may not be able to achieve
all of your use cases in a single database. For example, graph databases are
excellent for analyzing relationships in your data but may not provide what you
need for everyday retrieval of the data such as range queries. When selecting a
NoSQL database, consider what your use cases will be and if a general purpose
database like MongoDB would be a better option.
Python Programming By Sanjay Mate, IICMR-MCA
29
Sanjay Mate , Asst. Professor
IICMR - MCA
Sanjay Mate
Asst. professor
IICMR - MCA
Official Website :
https://www.mongodb.com/
Community Webserver :
https://www.mongodb.com/try/download/community
Python Programming By Sanjay Mate, IICMR-MCA
32
Python Programming By Sanjay Mate, IICMR-MCA
33
Python Programming By Sanjay Mate, IICMR-MCA
34
Python Programming By Sanjay Mate, IICMR-MCA
35
Python Programming By Sanjay Mate, IICMR-MCA
36
db.coll.insertOne({name: "Max"})
db.coll.insert([{name: "Max"}, {name:"Alex"}])
db.coll.insert([{name: "Max"}, {name:"Alex"}], {ordered:
false})
db.coll.insert({date: ISODate()})
db.coll.insert({name: "Max"}, {"writeConcern": {"w":
"majority", "wtimeout": 5000}})
Python Programming By Sanjay Mate, IICMR-MCA
37
Python Programming By Sanjay Mate, IICMR-MCA
38
Python Programming By Sanjay Mate, IICMR-MCA
39
Python Programming By Sanjay Mate, IICMR-MCA
40
Python Programming By Sanjay Mate, IICMR-MCA
41
Python Programming By Sanjay Mate, IICMR-MCA
42
Sanjay Mate
Asst. professor
IICMR - MCA
C- Create R-Read U-Update D-Delete
• insert ( ) • updateOne ( )
• deleteOne ( )
• insertOne ( ) • find( ) • updateMany ( )
• deleteMany ( )
• insertMany ( ) • replaceOne ( )
CRUD SQL
Create INSERT
Read SELECT
Update UPDATE
Delete DELETE
Python Programming By Sanjay Mate, IICMR-MCA
44
Create Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
45
Create Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
46
Create Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
47
Create Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
48
Create Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
49
drop Collection in database
Python Programming By Sanjay Mate, IICMR-MCA
50
C- Create
• insert ( )
Insert a Single Document
• insertOne ( )
• insertMany ( )
db.emp.insertOne( {empno:101, ename:”Sanjay”, sal : 50000, job: “Manager” , dept:10 } )
db.emp.insertOne( {empno:102, ename:”Rajesh”, sal : 20000, job: “Analyst” , dept:20 } )
db.emp.insertOne( {empno:103, ename:”Priti”, sal : 30000, job: “Analyst” , dept:10 } )
Python Programming By Sanjay Mate, IICMR-MCA
51
C- Create
• insert ( )
Insert Multiple Documents¶ • insertOne ( )
db.collection.insertMany( • insertMany ( )
[ <document 1> , <document 2>, ... ],
{
writeConcern: <document>,
ordered: <boolean>
}
)
db.emp.insertMany
(
[ {empno:104, ename:”Vilas”, sal : 50000, job: “Manager” , dept:10 } ,
{empno:105, ename:”Dinesh”, sal : 20000, job: “Analyst” , dept:10 } ,
{empno:106, ename:”Ruhi”, sal : 30000, job: “Developer” , dept:20 }
]
)
Python Programming By Sanjay Mate, IICMR-MCA
52
C- Create
• Load From
Java Script File
Save file as emp.js:
db.emp.insertOne(
{empno:101, ename:"Sanjay", sal : 50000, job: "Manager" , dept:10 } )
db.emp.insertOne(
{empno:102, ename:"Rajesh", sal : 20000, job: "Analyst" , dept:20 } )
db.emp.insertOne(
{empno:103, ename:"Priti", sal : 30000, job: "Analyst" , dept:10 } )
> load(“emp.js”)
Python Programming By Sanjay Mate, IICMR-MCA
53
Python Programming By Sanjay Mate, IICMR-MCA
54
Python Programming By Sanjay Mate, IICMR-MCA
55
Python Programming By Sanjay Mate, IICMR-MCA
56
Python Programming By Sanjay Mate, IICMR-MCA
57
Python Programming By Sanjay Mate, IICMR-MCA
58
Python Programming By Sanjay Mate, IICMR-MCA
59
Python Programming By Sanjay Mate, IICMR-MCA
60
Python Programming By Sanjay Mate, IICMR-MCA
61
Python Programming By Sanjay Mate, IICMR-MCA
62
Python Programming By Sanjay Mate, IICMR-MCA
65
Sanjay Mate
Asst. professor
IICMR - MCA
Install pymongo
https://pypi.org/project/pymongo/
Python Programming By Sanjay Mate, IICMR-MCA
75
Python Programming By Sanjay Mate, IICMR-MCA
76
Sanjay Mate , Asst. Professor
IICMR - MCA
Sanjay Mate
Asst. professor
IICMR - MCA
Pandas is a Python library used for
working with data sets.
It has functions for analyzing, cleaning,
exploring, and manipulating data.
Python Programming By Sanjay Mate, IICMR-MCA
79
Pandas allows us to analyze big data and make
conclusions based on statistical theories.
Pandas can clean messy data sets, and make them
readable and relevant.
Relevant data is very important in data science.
Python Programming By Sanjay Mate, IICMR-MCA
80
• Is there a correlation between two or more
columns?
• What is average value?
• Max value?
• Min value?
Python Programming By Sanjay Mate, IICMR-MCA
81
Topics to be Covered:
• Import ‘pymongo’ module
• Create MongoDb client object.
• Connecting MongoDB database using Python.
• List the Database and collections.
Python Programming By Sanjay Mate, IICMR-MCA
82
Sanjay Mate , Asst. Professor
IICMR - MCA