https://www.halvorsen.
blog
Python and
MongoDB
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Contents
• MongoDB
– MongoDB Community Server
– MongoDB Atlas
– MongoDB Compass
• PyMongo Python Driver/Library
• Python Examples
– Create and Retrieve Data
– Modify and Delete Data
– Datalogging Example
MongoDB
• MongoDB is a cross-platform document-oriented database program.
• MongoDB is a NoSQL database program
• MongoDB uses JSON-like documents
• Home Page: https://www.mongodb.com/
Software:
• MongoDB Community Server – Free version of the MongoDB Server
which can be installed locally on your computer or a server
• MongoDB Atlas – Premade MongoDB ready to use in the Cloud
• MongoDB Compass – GUI for connecting to and manipulating your
MongoDB database
• PyMongo – MongoDB Driver for Python
MongoDB Community Server
• Free version of the MongoDB Server
• MongoDB Server can be installed
locally on your computer or on an
external server
https://www.mongodb.com/try/download/community
MongoDB Community Server will be used in this Tutorial. So just download and
install the MongoDB Community Server, then you are ready to follow this Tutorial
MongoDB Atlas
• Premade MongoDB ready to use in
the Cloud
• You can use a Shared Clusters for free
• Purpose: Learning MongoDB or
developing small applications
https://www.mongodb.com/cloud/atlas
MongoDB Compass
• MongoDB Compass is the official Graphical User
Interface (GUI) for MongoDB
• With MongoDB Compass you can explore and
manipulate your MongoDB data
• To use Compass, you must connect to an
existing MongoDB database. You can connect to:
– A MongoDB server that you have installed locally,
or
– A MongoDB Atlas cluster.
MongoDB Compass
MongoDB Compass
PyMongo
• The PyMongo package contains tools for
interacting with MongoDB database
from Python
• The PyMongo package is a native
Python driver for MongoDB
• Install using PIP: pip install pymongo
• https://pypi.org/project/pymongo/
PyMongo Installation
SQL vs MongoDB
Note the following:
• A collection in MongoDB is the same
as a table in SQL databases.
• A document in MongoDB is the same
as a record in SQL databases.
https://www.halvorsen.blog
Python Examples
Hans-Petter Halvorsen
Database CRUD
All Database Systems supports CRUD
C – Create or Insert Data
R – Retrieve Data
U – Update Data
D – Delete Data
Let's go through some Python examples
https://www.halvorsen.blog
Insert Data
Hans-Petter Halvorsen
Python
Python script that creates a Database (“Library”), a Collection (“BookDB”) and a Document.
In a SQL database we use the INSERT statement to insert data in a table.
In MongoDB we use the insert_one() and insert_many() methods to insert data into a collection.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
document = { "Title": "C# Programming", "Author": "Knut Hamsun" }
x = collection.insert_one(document)
MongoDB Compass
Insert Multiple Documents
• To insert a record, or document as it is called in MongoDB, into a collection, we use
the insert_one() method.
• To insert multiple documents into a collection in MongoDB, we use the insert_many()
method.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
documents = [
{ "Title": "C# Programming", "Author": "Knut Hamsun" },
{ "Title": "ASP.NET Core", "Author": "Henrik Ibsen" },
{ "Title": "Python Basics", "Author": "Sigrid Undset" }
]
x = collection.insert_many(documents)
MongoDB Compass
https://www.halvorsen.blog
Retrieve Data
Hans-Petter Halvorsen
Retrieve Data
In a SQL database we use the SELECT to retrieve data in a table.
In MongoDB we use the find() and find_one() methods to find data in a collection.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
x = collection.find_one()
print(x)
{'_id': ObjectId('608028fc9708acadbcecc811'), 'Title': 'C#
Programming', 'Author': 'Knut Hamsun'}
Retrieve All Data
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
for x in collection.find():
print(x)
{'_id': ObjectId('608028fc9708acadbcecc811'), 'Title': 'C#
Programming', 'Author': 'Knut Hamsun'}
{'_id': ObjectId('608028fc9708acadbcecc812'), 'Title':
'ASP.NET Core', 'Author': 'Henrik Ibsen'}
{'_id': ObjectId('608028fc9708acadbcecc813'), 'Title':
'Python Basics', 'Author': 'Sigrid Undset'}
Retrieve specific Data
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
query = { "Author": "Knut Hamsun" }
documents = collection.find(query)
for x in documents:
print(x)
{'_id': ObjectId('608028fc9708acadbcecc811'), 'Title': 'C#
Programming', 'Author': 'Knut Hamsun'}
Sort the Results
Use the sort() method to sort the result in ascending or descending order.
The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending
is the default direction).
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
documents = collection.find().sort("Title")
for x in documents:
print(x)
{'_id': ObjectId('608028fc9708acadbcecc812'), 'Title': 'ASP.NET Core', 'Author': 'Henrik
Ibsen'}
{'_id': ObjectId('608028fc9708acadbcecc811'), 'Title': 'C# Programming', 'Author': 'Knut
Hamsun'}
{'_id': ObjectId('608028fc9708acadbcecc813'), 'Title': 'Python Basics', 'Author':
'Sigrid Undset'}
https://www.halvorsen.blog
Update Data
Hans-Petter Halvorsen
Update Data
You can update a record, or document as it is called in MongoDB, by using the
update_one() method.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
query = { "Title": "C# Programming" }
newvalue = { "$set": { "Title": "C# Web Programming" } }
collection.update_one(query, newvalue)
documents = collection.find()
for x in documents:
print(x)
Update Data
We can also Update Data in the Database using the MongoDB Compass
https://www.halvorsen.blog
Delete Data
Hans-Petter Halvorsen
Delete Data
You can delete a record, or document as it is called in MongoDB, by using the
delete_one() method.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["Library"]
collection = database["Book"]
query = { "Title": "C# Programming" }
collection.delete_one(query)
documents = collection.find()
for x in documents:
print(x)
Delete Data
We can also Update Data in the Database using the MongoDB Compass
https://www.halvorsen.blog
Datalogging Example
Hans-Petter Halvorsen
Datalogging Example
• We can log data from a DAQ device or
similar
• We start by creating a simple Random
Generator that simulates a Temperature
Sensor and log these data to the
MongoDB database
• Then we will in another script read the
data from the database and plot them.
import pymongo
import random
import time
Logging Data from datetime import datetime
# Create Database
client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["MeasurementSystem"]
collection = database["MeasurementData"]
Ts = 10 # Sampling Time
N = 10
for k in range(N):
# Generate Random Data
LowLimit = 20
UpperLimit = 25
MeasurementValue = random.randint(LowLimit, UpperLimit)
#Find Date and Time
now = datetime.now()
datetimeformat = "%Y-%m-%d %H:%M:%S"
MeasurementDateTime = now.strftime(datetimeformat)
# Insert Data into Database
document = { "MeasurementValue": MeasurementValue, "MeasurementDateTime":
MeasurementDateTime }
x = collection.insert_one(document)
# Wait
time.sleep(Ts)
Logged Data
import pymongo
import matplotlib.pyplot as plt
from datetime import datetime
# Connect to Database
Plotting Data client = pymongo.MongoClient("mongodb://localhost:27017/")
database = client["MeasurementSystem"]
collection = database["MeasurementData"]
t = []
data = []
# Retrieving and Formatting Data
for document in collection.find():
MeasurementValue = document["MeasurementValue"]
MeasurementDateTime = document["MeasurementDateTime"]
timeformat = "%Y-%m-%d %H:%M:%S"
MeasurementDateTime = datetime.strptime(MeasurementDateTime, timeformat)
data.append(MeasurementValue)
t.append(MeasurementDateTime)
# Plotting
plt.plot(t, data, 'o-')
plt.title('Temperature')
plt.xlabel('t [s]')
plt.ylabel('Temp [degC]')
plt.grid()
plt.show()
Plotted Data
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog