Enforcing a schema
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
From flexible to validated schema
MongoDB lets you store data without a fixed schema
Great for rapid prototyping and evolving your data model
mov.insert_one({
"title": "knives out",
"genre": ["comedy", "crime", "drama"],
"year": 2019, # oops
"rating": 7.9,
})
mov.find_one({
"title": "knives out",
"release_year": 2019
})
When schema is clear, important to configure validation
INTRODUCTION TO MONGODB IN PYTHON
Enforce a schema with pydantic
Data validation library
from pydantic import BaseModel
Define expected fields and their types from typing import Optional
Blueprint for every document
class Movie(BaseModel):
title: str
genre: list[str]
release_year: int
rating: float
won_oscar: Optional[bool] = None
INTRODUCTION TO MONGODB IN PYTHON
Inserting with typed data models
# Before # Now
new_movie = { new_movie = Movie(
"title": "knives out", title = "knives out",
"genre": ["comedy", "crime", "drama"], genre = ["comedy", "crime", "drama"],
"year": 2019, # oops year = 2019, # oops
"rating": 7.9, rating = 7.9,
} )
# No output pydantic.error_wrappers.ValidationError:
1 validation error for Movie
No checks on the data format release_year: field required
Typos and missing fields are caught before
they make it into the collection!
INTRODUCTION TO MONGODB IN PYTHON
Fixing our mistake
from pydantic import BaseModel # Correct set of fields and field values
from typing import Optional new_movie = Movie(
title = "knives out",
class Movie(BaseModel): genre = ["comedy", "crime", "drama"],
title: str release_year = 2019,
genre: list[str] rating = 7.9,
release_year: int )
rating: float
won_oscar: Optional[bool] = None mov.insert_one(dict(new_movie))
InsertOneResult(...)
INTRODUCTION TO MONGODB IN PYTHON
MongoDB's built-in schema validation
[Link].create_collection(
"movies_v2",
validator={
"$jsonSchema": {
"required": ["title", "genre", "release_year", "rating"],
"properties": {
"title": { "bsonType": "string" },
"genre": {
"bsonType": "array",
"items": { "bsonType": "string" }
},
"release_year": { "bsonType": "int" },
"rating": { "bsonType": "double" },
"won_oscar": { "bsonType": "bool" }
}
}
}
)
INTRODUCTION TO MONGODB IN PYTHON
Testing MongoDB's built-in schema validation
[Link].movies_v2.insert_one({
"title": "knives out",
"genre": ["comedy", "crime", "drama"],
"year": 2019, # oops
"rating": 7.9,
})
[Link]: Document failed validation, [...]
'missingProperties': ['release_year'], 'errmsg': 'Document failed validation'}
Schema validation at the database level
Works across all applications accessing MongoDB
INTRODUCTION TO MONGODB IN PYTHON
Summary
Application-side validation: [Link]
Database-side validation: MongoDB's built-in schema validation
Prevent mistakes
Enforce structure
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Combining
MongoDB
operations
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Building a Bulk Operation
from pymongo import InsertOne, UpdateOne # import classes
operations = [
InsertOne({ # operation 1
"title": "Dune",
"genre": ["action", "adventure", "drama"],
"release_year": 2021,
"rating": 8.0,
"won_oscar": False
}),
UpdateOne( # operation 2
{ "title": "Titanic" },
{ "$set": { "won_oscar": True } }
)
]
result = mov.bulk_write(operations) # pass list to bulk_write
INTRODUCTION TO MONGODB IN PYTHON
Understanding the result
print(result.inserted_count)
print(result.modified_count)
Keep in mind
Executed in order: earlier operations are not rolled back
Want stricter safety? Check out transactions
INTRODUCTION TO MONGODB IN PYTHON
Why use bulk_write?
Efficient
Send all operations in a single request
Saves time, reduced network overhead
Trackable
Single result object
Unified summary of results of all operations
Easier to audit and debug
Clean
Avoids scattering operations
Keeps logic grouped and easier to read
INTRODUCTION TO MONGODB IN PYTHON
What can you combine?
Use any mix of:
InsertOne()
UpdateOne() / UpdateMany() / ReplaceOne()
DeleteOne() / DeleteMany()
ReplaceOne()
Great for:
Syncing data
Cleaning up datasets
Bulk imports
INTRODUCTION TO MONGODB IN PYTHON
Let's practice!
INTRODUCTION TO MONGODB IN PYTHON
Final thoughts
INTRODUCTION TO MONGODB IN PYTHON
Filip Schouwenaars
Machine Learning Researcher
Chapter 1 recap
Relational databases vs. MongoDB
Connected to MongoDB locally
Queried databases and collections
Retrieved documents using .find() and .find_one()
Simple filters, e.g., { "title": "Inception" }
INTRODUCTION TO MONGODB IN PYTHON
Chapter 2 recap
Filter documents using $exists , $in , $or , $and
Control output with limit() , sort() , and projection
Count matching documents with .count_documents()
INTRODUCTION TO MONGODB IN PYTHON
Chapter 3 recap
Insert new documents: .insert_one() and .insert_many()
Update documents: .update_one() and .update_many()
Replace documents: replace_one()
Delete documents: delete_one() , delete_many()
INTRODUCTION TO MONGODB IN PYTHON
Chapter 4 recap
Combine Python typing with pydantic
MongoDB's built-in schema validation
Manage documents with confidence
Combine operations with .bulk_write() for efficiency
INTRODUCTION TO MONGODB IN PYTHON
Where to go next
Launch your own database with MongoDB Atlas
Deploy, scale and secure without managing servers yourself
Build real-world applications using PyMongo and Atlas
Continue leveling up with MongoDB:
MongoDB Skill Badges: [Link]
MongoDB Certification: [Link]
Official PyMongo docs: [Link]
MongoDB University: [Link]
INTRODUCTION TO MONGODB IN PYTHON
Congratulations!
INTRODUCTION TO MONGODB IN PYTHON