Inserting documents
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Why insert data?
Data needs to exist in your database
Need way to add it!
Two methods
insert_one() : add single document
insert_many() : add list of documents
INTRODUCTION TO MONGODB IN PYTHON
Inserting a single document
from pymongo import MongoClient result = mov.insert_one(new_movie)
client = MongoClient()
mov = [Link] Unique _id is automatically generated!
mov.count_documents({})
result.inserted_id
41
ObjectId('6837575d10d855c9f6698341')
new_movie = {
mov.count_documents({})
"title": "oppenheimer",
"genre": ["drama", "history"],
"release_year": 2023, 42
"rating": 8.7
}
INTRODUCTION TO MONGODB IN PYTHON
Inserting multiple documents
new_movies = [ result = mov.insert_many(new_movies)
{ print(result.inserted_ids)
"title": "the holdovers",
"genre": ["comedy", "drama"], [ObjectId("662ecfe2e89c44eb19ae1234"),
"release_year": 2023, ObjectId("662ecfe2e89c44eb19ae1235")]
"rating": 7.9
}, All _id s are auto-generated.
{
"title": "past lives",
"genre": ["drama", "romance"],
"release_year": 2023,
"rating": 7.8
}
]
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Updating a single
document
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Why update or replace?
After adding, data evolves
Correct mistakes
Add missing info
Restructure entire documents
MongoDB provides tools to update and replace
INTRODUCTION TO MONGODB IN PYTHON
Updating a single document
# Define filter to find document to change
query_filter = { "title": "la la land" }
# Describe the change
update = { "$set": { "release_year": 2016 } }
# Perform update
res = mov.update_one(query_filter, update)
res.modified_count
INTRODUCTION TO MONGODB IN PYTHON
Updating multiple documents
# Define filter (can match multiple documents!)
query_filter = { "genre": "comedy" }
# Describe the change
update = { "$set": { "is_funny": True } }
# Apply change to all matching documents
result = mov.update_many(query_filter, update)
print(result.modified_count)
INTRODUCTION TO MONGODB IN PYTHON
Replacing a full document
query_filter = { "title": "the lion king" } replacement = {
mov.find_one(query_filter) "title": "the lion king",
"genre": ["animation", "adventure", "drama"],
"release_year": 2019,
{
"rating": 6.8
'_id': '68375bf5a63f71e478cdc7f5'
}
'title': 'the lion king',
'release_year': 1994,
mov.replace_one(query_filter, replacement)
...
mov.find_one(query_filter)
}
{
'_id': '68375bf5a63f71e478cdc7f5',
'title': 'the lion king'
'release_year': 2019,
...
}
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Deleting documents
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Deleting a single document
# Setup
from pymongo import MongoClient
client = MongoClient()
mov = [Link]
# Delete document that matches filter, similar to find_one()
res = mov.delete_one({ "title": "gladiator" })
# How many documents were deleted?
res.deleted_count
INTRODUCTION TO MONGODB IN PYTHON
Removing multiple documents at the same time
# Delete all really bad movies
res = mov.delete_many({ "rating": { "$lt": 5.0 } })
# How many documents deleted?
res.deleted_count
Deleting documents is permanent and cannot be undone!
Double check your filters before deleting!
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON