SlideShare a Scribd company logo
Using Mongo Databases in Python
Arun A V
Institution: Model Engineering College, Kochi
May, 2022
Arun A V Using Mongo Databases in Python May, 2022 1 / 11
Introduction
MongoDB is a document based database with a dynamic data
schema
The JavaScript Object Notation (JSON) that it supports is a
natural fit for working with objects in modern programming
languages like JavaScript, Python and others
This provides an alternative to more traditional Relational
Database Management Systems (RDBMS) such as SQL.
MongoDB is an example of a NoSQL databases
MongoDB stores data as collections of documents using
key/value pairs
Arun A V Using Mongo Databases in Python May, 2022 2 / 11
Introduction
Arun A V Using Mongo Databases in Python May, 2022 3 / 11
Introduction
Unlike relational databases where we would have to create a new
column in a table to store information, data can be embedded
Arun A V Using Mongo Databases in Python May, 2022 4 / 11
Getting Started I
To use pymongo, you first need to install the library, for example with
pip in the Python prompt
pip install pymongo
Next, we need to import the pymongo library into a Python file or
Jupyter notebook.
import pymongo
And then connect to a Mongo client. This connects on the default
host and port.
client = pymongo.MongoClient(“mongodb://localhost:27017/”)
We can then create a database to store some data. In this example
it’s going to store some details of patients for a health system.
db = client[“meddata”]
Arun A V Using Mongo Databases in Python May, 2022 5 / 11
Getting Started II
Next, we can add a collection to that database. Each database can
contain multiple collections. This collection will be called patientdata
and we will reference the collection in Python using the variable
mycollection.
mycollection = db[”patientdata”]
Arun A V Using Mongo Databases in Python May, 2022 6 / 11
Inserting data I
We can then add some data (a document) to the collection. Let’s
say we wanted to store some basic details about a patient
patientrecord = ”Name”: ”Maureen Skinner”, ”Age”: 87,
”Sex”: ”F”, ”Blood pressure”: [”sys”: 156, ”dia”: 82], ”Heart
rate”: 82
Multiple documents can be added by simply adding a comma after
the closing brace and adding additional objects. The different objects
can also contain completely different data fields as required.
Once we have created a document(s), we can add it to the collection.
To add a single document we first specify the collection we want to
add in to followed by a dot then we can use the insert one function
(for many we use the insert many) passing in the document object
variable:
mycollection.insert one(patientrecord)
Arun A V Using Mongo Databases in Python May, 2022 7 / 11
Inserting data II
To view the contents of the collection we can loop over each item of
the collection and print it.
for item in mycollection.find(): print(item)
This will output the data like so:
We can add multiple records at a time using the insert many
function:
Arun A V Using Mongo Databases in Python May, 2022 8 / 11
Inserting data III
patientrecords = [ ”Name”: ”Adam Blythe”, ”Age”: 55, ”Sex”:
”M”, ”Blood pressure”: [”sys”: 132, ”dia”: 73], ”Heart rate”:
73 , ”Name”: ”Darren Sanders”, ”Age”: 34, ”Sex”: ”M”,
”Blood pressure”: [”sys”: 120, ”dia”: 70], ”Heart rate”: 67 ,
”Name”: ”Sally-Ann Joyce”, ”Age”: 19, ”Sex”: ”F”, ”Blood
pressure”: [”sys”: 121, ”dia”: 72], ”Heart rate”: 67 ]
mycollection.insert many(patientrecords)
Arun A V Using Mongo Databases in Python May, 2022 9 / 11
Updating data
We may also want to update data that we have previously added to a
collection. Again we can update a single or multiple records. Let’s
say we accidentally added the same heart rate for Darren Sanders and
Sally-Ann Joyce. Darren’s was supposed to be 88.
mycollection.update one(”Name”: ”Darren Sanders”,
”$set”:”Heart rate”: 88)
Arun A V Using Mongo Databases in Python May, 2022 10 / 11
Thank You !
Arun A V Using Mongo Databases in Python May, 2022 11 / 11

More Related Content

Similar to Mongodb.pdf

BigML.io - The BigML API
BigML.io - The BigML APIBigML.io - The BigML API
BigML.io - The BigML API
Francisco J Martin
 
Oracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivityOracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivity
rohit vishwakarma
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
S.Shayan Daneshvar
 
Unit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptxUnit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptx
Malla Reddy University
 
Fake News and Their Detection
Fake News and Their DetectionFake News and Their Detection
Fake News and Their Detection
Kourosh Sajjadi
 
IBM Cognos tutorial - ABC LEARN
IBM Cognos tutorial - ABC LEARNIBM Cognos tutorial - ABC LEARN
IBM Cognos tutorial - ABC LEARN
abclearnn
 
Webinar: Mastering Python - An Excellent tool for Web Scraping and Data Anal...
Webinar:  Mastering Python - An Excellent tool for Web Scraping and Data Anal...Webinar:  Mastering Python - An Excellent tool for Web Scraping and Data Anal...
Webinar: Mastering Python - An Excellent tool for Web Scraping and Data Anal...
Edureka!
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Kavika Roy
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
Prashanth Panduranga
 
Using pandas library for data analysis in python
Using pandas library for data analysis in pythonUsing pandas library for data analysis in python
Using pandas library for data analysis in python
Bruce Jenks
 
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
MongoDB
 
Search as-you-type (Exact search)
Search as-you-type (Exact search)Search as-you-type (Exact search)
Search as-you-type (Exact search)
Gabani Bhavik
 
Database Essay
Database EssayDatabase Essay
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on python
kannikadg
 
Getting your data in and out of elasticsearch: let me count the ways
Getting your data in and out of elasticsearch: let me count the waysGetting your data in and out of elasticsearch: let me count the ways
Getting your data in and out of elasticsearch: let me count the ways
🥑 Jay Miller
 
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQueryIntro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Chris Schalk
 
Big data-analytics-cpe8035
Big data-analytics-cpe8035Big data-analytics-cpe8035
Big data-analytics-cpe8035
Neelam Rawat
 
A survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo dbA survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo db
Alexander Decker
 
A survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo dbA survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo db
Alexander Decker
 

Similar to Mongodb.pdf (20)

BigML.io - The BigML API
BigML.io - The BigML APIBigML.io - The BigML API
BigML.io - The BigML API
 
Oracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivityOracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivity
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Unit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptxUnit 2 - Data Manipulation with R.pptx
Unit 2 - Data Manipulation with R.pptx
 
Fake News and Their Detection
Fake News and Their DetectionFake News and Their Detection
Fake News and Their Detection
 
IBM Cognos tutorial - ABC LEARN
IBM Cognos tutorial - ABC LEARNIBM Cognos tutorial - ABC LEARN
IBM Cognos tutorial - ABC LEARN
 
Webinar: Mastering Python - An Excellent tool for Web Scraping and Data Anal...
Webinar:  Mastering Python - An Excellent tool for Web Scraping and Data Anal...Webinar:  Mastering Python - An Excellent tool for Web Scraping and Data Anal...
Webinar: Mastering Python - An Excellent tool for Web Scraping and Data Anal...
 
Building Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata CatalogBuilding Your First MongoDB App ~ Metadata Catalog
Building Your First MongoDB App ~ Metadata Catalog
 
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
 
Using pandas library for data analysis in python
Using pandas library for data analysis in pythonUsing pandas library for data analysis in python
Using pandas library for data analysis in python
 
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
MongoDB Evenings Houston: What's the Scoop on MongoDB and Hadoop? by Jake Ang...
 
Search as-you-type (Exact search)
Search as-you-type (Exact search)Search as-you-type (Exact search)
Search as-you-type (Exact search)
 
Database Essay
Database EssayDatabase Essay
Database Essay
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on python
 
Getting your data in and out of elasticsearch: let me count the ways
Getting your data in and out of elasticsearch: let me count the waysGetting your data in and out of elasticsearch: let me count the ways
Getting your data in and out of elasticsearch: let me count the ways
 
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQueryIntro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
Intro to new Google cloud technologies: Google Storage, Prediction API, BigQuery
 
Big data-analytics-cpe8035
Big data-analytics-cpe8035Big data-analytics-cpe8035
Big data-analytics-cpe8035
 
A survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo dbA survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo db
 
A survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo dbA survey on data mining and analysis in hadoop and mongo db
A survey on data mining and analysis in hadoop and mongo db
 

Recently uploaded

UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-IDUNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
GOWSIKRAJA PALANISAMY
 
Benefits of Studying Artificial Intelligence - KRCE.pptx
Benefits of Studying Artificial Intelligence - KRCE.pptxBenefits of Studying Artificial Intelligence - KRCE.pptx
Benefits of Studying Artificial Intelligence - KRCE.pptx
krceseo
 
Time-State Analytics: MinneAnalytics 2024 Talk
Time-State Analytics: MinneAnalytics 2024 TalkTime-State Analytics: MinneAnalytics 2024 Talk
Time-State Analytics: MinneAnalytics 2024 Talk
Evan Chan
 
Conservation of Natural Resources Biodiversity.pptx
Conservation of Natural Resources Biodiversity.pptxConservation of Natural Resources Biodiversity.pptx
Conservation of Natural Resources Biodiversity.pptx
AdarshaMR1
 
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
IJAEMSJORNAL
 
IWISS Catalog 2024
IWISS Catalog 2024IWISS Catalog 2024
IWISS Catalog 2024
Iwiss Tools Co.,Ltd
 
Response & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITHResponse & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITH
IIIT Hyderabad
 
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptxPresentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Er. Kushal Ghimire
 
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
PradeepKumarSK3
 
Synthetic Test Collections for Retrieval Evaluation (Poster)
Synthetic Test Collections for Retrieval Evaluation (Poster)Synthetic Test Collections for Retrieval Evaluation (Poster)
Synthetic Test Collections for Retrieval Evaluation (Poster)
Hossein A. (Saeed) Rahmani
 
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
IJAEMSJORNAL
 
Evento anual Splunk .conf24 Highlights recap
Evento anual Splunk .conf24 Highlights recapEvento anual Splunk .conf24 Highlights recap
Evento anual Splunk .conf24 Highlights recap
Rafael Santos
 
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
sanabts249
 
Monitoring and reporting of transparent forest data and information under the...
Monitoring and reporting of transparent forest data and information under the...Monitoring and reporting of transparent forest data and information under the...
Monitoring and reporting of transparent forest data and information under the...
Pilar Valbuena Perez
 
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmtlecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
RAtna29
 
RECENT DEVELOPMENTS IN RING SPINNING.pptx
RECENT DEVELOPMENTS IN RING SPINNING.pptxRECENT DEVELOPMENTS IN RING SPINNING.pptx
RECENT DEVELOPMENTS IN RING SPINNING.pptx
peacesoul123
 
Rotary Intersection in traffic engineering.pptx
Rotary Intersection in traffic engineering.pptxRotary Intersection in traffic engineering.pptx
Rotary Intersection in traffic engineering.pptx
surekha1287
 
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
Jim Mimlitz, P.E.
 
Chlorine and Nitric Acid application, properties, impacts.pptx
Chlorine and Nitric Acid application, properties, impacts.pptxChlorine and Nitric Acid application, properties, impacts.pptx
Chlorine and Nitric Acid application, properties, impacts.pptx
yadavsuyash008
 
OSHA LOTO training, LOTO, lock out tag out
OSHA LOTO training, LOTO, lock out tag outOSHA LOTO training, LOTO, lock out tag out
OSHA LOTO training, LOTO, lock out tag out
Ateeb19
 

Recently uploaded (20)

UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-IDUNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
UNIT I INCEPTION OF INFORMATION DESIGN 20CDE09-ID
 
Benefits of Studying Artificial Intelligence - KRCE.pptx
Benefits of Studying Artificial Intelligence - KRCE.pptxBenefits of Studying Artificial Intelligence - KRCE.pptx
Benefits of Studying Artificial Intelligence - KRCE.pptx
 
Time-State Analytics: MinneAnalytics 2024 Talk
Time-State Analytics: MinneAnalytics 2024 TalkTime-State Analytics: MinneAnalytics 2024 Talk
Time-State Analytics: MinneAnalytics 2024 Talk
 
Conservation of Natural Resources Biodiversity.pptx
Conservation of Natural Resources Biodiversity.pptxConservation of Natural Resources Biodiversity.pptx
Conservation of Natural Resources Biodiversity.pptx
 
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
Profiling of Cafe Business in Talavera, Nueva Ecija: A Basis for Development ...
 
IWISS Catalog 2024
IWISS Catalog 2024IWISS Catalog 2024
IWISS Catalog 2024
 
Response & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITHResponse & Safe AI at Summer School of AI at IIITH
Response & Safe AI at Summer School of AI at IIITH
 
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptxPresentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
Presentation slide on DESIGN AND FABRICATION OF MOBILE CONTROLLED DRAINAGE.pptx
 
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
21EC63_Module1B.pptx VLSI design 21ec63 MOS TRANSISTOR THEORY
 
Synthetic Test Collections for Retrieval Evaluation (Poster)
Synthetic Test Collections for Retrieval Evaluation (Poster)Synthetic Test Collections for Retrieval Evaluation (Poster)
Synthetic Test Collections for Retrieval Evaluation (Poster)
 
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
Best Practices of Clothing Businesses in Talavera, Nueva Ecija, A Foundation ...
 
Evento anual Splunk .conf24 Highlights recap
Evento anual Splunk .conf24 Highlights recapEvento anual Splunk .conf24 Highlights recap
Evento anual Splunk .conf24 Highlights recap
 
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
21CV61- Module 3 (CONSTRUCTION MANAGEMENT AND ENTREPRENEURSHIP.pptx
 
Monitoring and reporting of transparent forest data and information under the...
Monitoring and reporting of transparent forest data and information under the...Monitoring and reporting of transparent forest data and information under the...
Monitoring and reporting of transparent forest data and information under the...
 
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmtlecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
lecture10-efficient-scoring.ppmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmt
 
RECENT DEVELOPMENTS IN RING SPINNING.pptx
RECENT DEVELOPMENTS IN RING SPINNING.pptxRECENT DEVELOPMENTS IN RING SPINNING.pptx
RECENT DEVELOPMENTS IN RING SPINNING.pptx
 
Rotary Intersection in traffic engineering.pptx
Rotary Intersection in traffic engineering.pptxRotary Intersection in traffic engineering.pptx
Rotary Intersection in traffic engineering.pptx
 
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
SCADAmetrics Instrumentation for Sensus Water Meters - Core and Main Training...
 
Chlorine and Nitric Acid application, properties, impacts.pptx
Chlorine and Nitric Acid application, properties, impacts.pptxChlorine and Nitric Acid application, properties, impacts.pptx
Chlorine and Nitric Acid application, properties, impacts.pptx
 
OSHA LOTO training, LOTO, lock out tag out
OSHA LOTO training, LOTO, lock out tag outOSHA LOTO training, LOTO, lock out tag out
OSHA LOTO training, LOTO, lock out tag out
 

Mongodb.pdf

  • 1. Using Mongo Databases in Python Arun A V Institution: Model Engineering College, Kochi May, 2022 Arun A V Using Mongo Databases in Python May, 2022 1 / 11
  • 2. Introduction MongoDB is a document based database with a dynamic data schema The JavaScript Object Notation (JSON) that it supports is a natural fit for working with objects in modern programming languages like JavaScript, Python and others This provides an alternative to more traditional Relational Database Management Systems (RDBMS) such as SQL. MongoDB is an example of a NoSQL databases MongoDB stores data as collections of documents using key/value pairs Arun A V Using Mongo Databases in Python May, 2022 2 / 11
  • 3. Introduction Arun A V Using Mongo Databases in Python May, 2022 3 / 11
  • 4. Introduction Unlike relational databases where we would have to create a new column in a table to store information, data can be embedded Arun A V Using Mongo Databases in Python May, 2022 4 / 11
  • 5. Getting Started I To use pymongo, you first need to install the library, for example with pip in the Python prompt pip install pymongo Next, we need to import the pymongo library into a Python file or Jupyter notebook. import pymongo And then connect to a Mongo client. This connects on the default host and port. client = pymongo.MongoClient(“mongodb://localhost:27017/”) We can then create a database to store some data. In this example it’s going to store some details of patients for a health system. db = client[“meddata”] Arun A V Using Mongo Databases in Python May, 2022 5 / 11
  • 6. Getting Started II Next, we can add a collection to that database. Each database can contain multiple collections. This collection will be called patientdata and we will reference the collection in Python using the variable mycollection. mycollection = db[”patientdata”] Arun A V Using Mongo Databases in Python May, 2022 6 / 11
  • 7. Inserting data I We can then add some data (a document) to the collection. Let’s say we wanted to store some basic details about a patient patientrecord = ”Name”: ”Maureen Skinner”, ”Age”: 87, ”Sex”: ”F”, ”Blood pressure”: [”sys”: 156, ”dia”: 82], ”Heart rate”: 82 Multiple documents can be added by simply adding a comma after the closing brace and adding additional objects. The different objects can also contain completely different data fields as required. Once we have created a document(s), we can add it to the collection. To add a single document we first specify the collection we want to add in to followed by a dot then we can use the insert one function (for many we use the insert many) passing in the document object variable: mycollection.insert one(patientrecord) Arun A V Using Mongo Databases in Python May, 2022 7 / 11
  • 8. Inserting data II To view the contents of the collection we can loop over each item of the collection and print it. for item in mycollection.find(): print(item) This will output the data like so: We can add multiple records at a time using the insert many function: Arun A V Using Mongo Databases in Python May, 2022 8 / 11
  • 9. Inserting data III patientrecords = [ ”Name”: ”Adam Blythe”, ”Age”: 55, ”Sex”: ”M”, ”Blood pressure”: [”sys”: 132, ”dia”: 73], ”Heart rate”: 73 , ”Name”: ”Darren Sanders”, ”Age”: 34, ”Sex”: ”M”, ”Blood pressure”: [”sys”: 120, ”dia”: 70], ”Heart rate”: 67 , ”Name”: ”Sally-Ann Joyce”, ”Age”: 19, ”Sex”: ”F”, ”Blood pressure”: [”sys”: 121, ”dia”: 72], ”Heart rate”: 67 ] mycollection.insert many(patientrecords) Arun A V Using Mongo Databases in Python May, 2022 9 / 11
  • 10. Updating data We may also want to update data that we have previously added to a collection. Again we can update a single or multiple records. Let’s say we accidentally added the same heart rate for Darren Sanders and Sally-Ann Joyce. Darren’s was supposed to be 88. mycollection.update one(”Name”: ”Darren Sanders”, ”$set”:”Heart rate”: 88) Arun A V Using Mongo Databases in Python May, 2022 10 / 11
  • 11. Thank You ! Arun A V Using Mongo Databases in Python May, 2022 11 / 11