Python MongoDB, Final Project & Interview Path
MongoDB stores JSON-like documents. Python uses PyMongo: pip install pymongo.
Python MongoDB — Get Started
MongoDB stores JSON-like documents. Python uses PyMongo: pip install pymongo.
Atlas free tier or local MongoDB for practice.
Real-life example: MongoDB is a flexible notebook — each page (document) can have different fields.
Create Database
MongoDB creates database on first write. client['knowvora'] selects or creates.
Real-life example: Naming a database is labeling a new shelf — items appear when you store the first box.
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["knowvora"]
print(db.name)Create Collection
Collections group documents — like tables without fixed schema.
db.create_collection('students') or implicit on insert.
Real-life example: A collection is a folder of similar forms — job applications, receipts, etc.
students = db["students"]
# or db.create_collection("students")Insert
insert_one({}) and insert_many([]) add documents with _id auto-generated.
Real-life example: Insert is dropping a new form into the folder.
students = db["students"]
result = students.insert_one({"name": "Aarav", "score": 88})
print(result.inserted_id)Find
find() returns a cursor; find_one() first match. Empty filter {} gets all.
Real-life example: Find is searching the folder for every form with score above 80.
for doc in students.find({"score": {"$gte": 80}}):
print(doc)Query
Query operators: $gt, $lt, $in, $regex. Combine with $and, $or.
Real-life example: Query is advanced search — name starts with A AND score > 70.
query = {"name": {"$regex": "^A"}, "score": {"$gt": 70}}
print(list(students.find(query)))Sort
sort('score', -1) orders descending. 1 = ascending.
Real-life example: Sort is arranging forms by highest score on top.
for doc in students.find().sort("score", -1):
print(doc["name"], doc["score"])Update
update_one(filter, {'$set': {...}}) changes fields. $inc increments numbers.
Real-life example: Update is correcting one field on a form without rewriting the whole page.
students.update_one(
{"name": "Aarav"},
{"$set": {"score": 92}},
)Delete
delete_one(filter) and delete_many(filter) remove documents.
Real-life example: Delete is throwing away outdated forms — keep backups in production.
students.delete_many({"score": {"$lt": 50}})Drop
drop() removes entire collection. drop_database() removes whole DB — dangerous.
Real-life example: Drop is shredding the whole folder, not one paper.
students.drop()Limit
limit(5) caps results — pair with sort() for top-N queries.
Real-life example: Limit is reading only the first five sorted results.
top = students.find().sort("score", -1).limit(3)
print(list(top))Final Project — Student Analytics CLI
Build a CLI that loads student JSON, computes stats, saves to MySQL or MongoDB, and plots scores with Matplotlib.
Combines files, OOP, databases, and visualization from this full course.
Real-life example: A school admin tool — import marks, store in DB, print chart for principal.
import json
import statistics
import matplotlib.pyplot as plt
with open("students.json", encoding="utf-8") as f:
data = json.load(f)
scores = [s["score"] for s in data]
print("Average:", statistics.mean(scores))
plt.bar([s["name"] for s in data], scores)
plt.title("Class Scores")
plt.show()Interview & Examples Path
After 40 lessons: practice /mcq/python-mcq, /interview/python-interview, and rebuild the final project without notes.
Review the examples in earlier lessons for one-liners on lists, RegEx, files, and sklearn.
Real-life example: Interview prep is dress rehearsal — same skills as the job stage, lower stakes.
- Know list/dict complexity and when to use set
- Explain train/test split and overfitting in plain English
- Write connect → insert → select for MySQL and MongoDB from memory
- Ship the student analytics CLI as your portfolio piece