MongoDB and PyMongo in Python


Introduction to MongoDB and PyMongo in Python

MongoDB is a NoSQL (non-relational) database designed for storing unstructured or semi-structured data in JSON-like documents called BSON. It's highly scalable and flexible, often used in modern web applications and big data systems.

PyMongo is the official MongoDB driver for Python.

Why Use MongoDB?

  • Schema-less (store varied data structures)
  • Uses JSON/BSON documents instead of rows/tables
  • Scalable and high-performance
  • Ideal for apps with dynamic or large datasets
  • Supports replication, sharding, and aggregation pipelines

Step 1: Install and Set Up MongoDB

Install MongoDB (Local)

Visit: https://www.mongodb.com/try/download/community

Or use MongoDB Atlas for cloud hosting.

Step 2: Install PyMongo

pip install pymongo

Step 3: Connect to MongoDB

from pymongo import MongoClient

# Localhost
client = MongoClient("mongodb://localhost:27017/")

# Create or switch to database
db = client["mydatabase"]

# Create or switch to collection
collection = db["users"]

CRUD Operations with PyMongo

Create - Insert Document

user = {"name": "Alice", "email": "alice@example.com"}
collection.insert_one(user)

Insert Many:

users = [
    {"name": "Bob", "email": "bob@example.com"},
    {"name": "Charlie", "email": "charlie@example.com"}
]
collection.insert_many(users)

Read - Find Documents

Find All
for user in collection.find():
    print(user)
Find One
user = collection.find_one({"name": "Alice"})
print(user)

Update - Modify Documents

collection.update_one({"name": "Alice"}, {"$set": {"email": "alice@new.com"}})
Update Many
collection.update_many({}, {"$set": {"active": True}})

Delete - Remove Documents

collection.delete_one({"name": "Bob"})
Delete Many
collection.delete_many({"active": True})

Other Useful MongoDB Operations

Count Documents

count = collection.count_documents({})
print("Total Users:", count)

Query with Conditions

results = collection.find({"name": {"$regex": "^A"}})
for r in results:
    print(r)

Projection (Limit Returned Fields)

for user in collection.find({}, {"_id": 0, "name": 1}):
    print(user)

Example App: Mini User Database

from pymongo import MongoClient

client = MongoClient("mongodb://localhost:27017/")
db = client["company"]
employees = db["employees"]

# Insert sample data
employees.insert_one({"name": "Emma", "role": "Developer", "salary": 60000})

# Query
for emp in employees.find():
    print(emp)

Bonus: Use MongoDB Atlas (Cloud)

Sign up at: https://cloud.mongodb.com/

Create a cluster.

Whitelist your IP & create a database user.

Use connection string like:

client = MongoClient("mongodb+srv://username:password@cluster.mongodb.net/?retryWrites=true&w=majority")

MongoDB vs SQL

Feature MongoDB (NoSQL) MySQL/PostgreSQL (SQL)
Data Structure Document (BSON/JSON) Table (rows/columns)
Schema Dynamic Fixed schema
Joins Limited ($lookup) Native support
Ideal for Big Data, Flexibility Structured Data

For large apps, consider using an ORM like SQLAlchemy or Django ORM.