MongoDB Python (PyMongo)
Work with MongoDB using Python's powerful PyMongo library
🐍 What is PyMongo?
PyMongo is the official MongoDB driver for Python. It provides a Pythonic way to interact with MongoDB databases, offering intuitive methods for database operations and seamless integration with Python's data structures and syntax.
# Simple PyMongo connection
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client['myDatabase']
print('Connected to MongoDB!')
Key Features
Simple Connection
Connect with Python's clean syntax
Dictionary-like
Work with familiar Python dictionaries
Query Builder
Build complex queries easily
Performance
Fast and efficient operations
🔹 Installation
Install PyMongo using pip, Python's package manager. This will download and install the latest stable version of PyMongo along with all its dependencies, making it ready to use in your Python projects.
# Install PyMongo
pip install pymongo
# Install with specific version
pip install pymongo==4.6.0
🔹 Connecting to MongoDB
Create a connection to MongoDB using MongoClient. You can connect to local or remote databases by specifying the connection string. Access databases and collections using dictionary-style syntax for a Pythonic experience.
from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
# Access database
db = client['myDatabase']
# Or using dot notation
db = client.myDatabase
# Access collection
collection = db['users']
# Or using dot notation
collection = db.users
print('Connected successfully!')
🔹 Insert Documents
Add documents to your MongoDB collections using Python dictionaries. PyMongo provides insert_one() for single documents and insert_many() for multiple documents, returning information about the inserted data including generated IDs.
# Insert one document
user = {
'name': 'John Doe',
'email': '[email protected]',
'age': 30,
'hobbies': ['reading', 'coding']
}
result = collection.insert_one(user)
print(f'Inserted ID: {result.inserted_id}')
# Insert multiple documents
users = [
{'name': 'Alice', 'email': '[email protected]', 'age': 25},
{'name': 'Bob', 'email': '[email protected]', 'age': 35}
]
result = collection.insert_many(users)
print(f'Inserted {len(result.inserted_ids)} documents')
Output:
Inserted ID: 507f1f77bcf86cd799439011
Inserted 2 documents
🔹 Find Documents
Query your MongoDB collections to retrieve documents using Python dictionaries as filters. Use find() to get multiple documents as a cursor or find_one() to retrieve a single document directly as a dictionary.
# Find all documents
all_users = collection.find()
for user in all_users:
print(user)
# Find with filter
adults = collection.find({'age': {'$gte': 18}})
for user in adults:
print(user['name'])
# Find one document
user = collection.find_one({'name': 'John Doe'})
print(f'Found: {user["name"]}')
# Find with projection (select specific fields)
names = collection.find({}, {'name': 1, 'email': 1, '_id': 0})
for doc in names:
print(doc)
Output:
{'name': 'John Doe', 'email': '[email protected]', 'age': 30}
Found: John Doe
🔹 Update Documents
Modify existing documents using update operations with Python dictionaries. PyMongo supports update_one() for single documents and update_many() for bulk updates, using MongoDB's update operators like $set and $inc.
# Update one document
result = collection.update_one(
{'name': 'John Doe'},
{'$set': {'age': 31, 'city': 'New York'}}
)
print(f'Modified: {result.modified_count}')
# Update many documents
result = collection.update_many(
{'age': {'$lt': 30}},
{'$set': {'status': 'young'}}
)
print(f'Updated {result.modified_count} documents')
# Increment a value
collection.update_one(
{'name': 'John Doe'},
{'$inc': {'age': 1}}
)
# Add to array
collection.update_one(
{'name': 'John Doe'},
{'$push': {'hobbies': 'gaming'}}
)
Output:
Modified: 1
Updated 2 documents
🔹 Delete Documents
Remove documents from your collection using delete operations. PyMongo provides delete_one() to remove a single matching document and delete_many() to remove all documents that match your filter criteria.
# Delete one document
result = collection.delete_one({'name': 'John Doe'})
print(f'Deleted: {result.deleted_count}')
# Delete many documents
result = collection.delete_many({'age': {'$lt': 18}})
print(f'Deleted {result.deleted_count} documents')
# Delete all documents
result = collection.delete_many({})
print(f'All deleted: {result.deleted_count}')
Output:
Deleted: 1
Deleted 3 documents
🔹 Aggregation Pipeline
Perform complex data analysis using aggregation pipelines. Build multi-stage pipelines with Python lists and dictionaries to filter, group, sort, and transform your data for advanced analytics and reporting.
# Aggregation pipeline
pipeline = [
{'$match': {'age': {'$gte': 18}}},
{'$group': {
'_id': '$city',
'avgAge': {'$avg': '$age'},
'count': {'$sum': 1}
}},
{'$sort': {'avgAge': -1}}
]
results = collection.aggregate(pipeline)
for doc in results:
print(f'{doc["_id"]}: avg age {doc["avgAge"]}, count {doc["count"]}')
Output:
New York: avg age 32, count 5
Boston: avg age 28, count 3
🔹 Indexing
Create indexes to improve query performance on frequently searched fields. PyMongo allows you to create single-field, compound, and text indexes to speed up data retrieval and enable efficient sorting operations.
# Create single field index
collection.create_index('email')
# Create compound index
collection.create_index([('name', 1), ('age', -1)])
# Create unique index
collection.create_index('email', unique=True)
# List all indexes
indexes = collection.list_indexes()
for index in indexes:
print(index)
🔹 Error Handling
Implement robust error handling to manage database exceptions and connection issues. PyMongo provides specific exception classes for different error types, allowing you to handle failures gracefully and maintain application stability.
from pymongo.errors import ConnectionFailure, DuplicateKeyError
try:
client = MongoClient('mongodb://localhost:27017/',
serverSelectionTimeoutMS=5000)
# Test connection
client.admin.command('ping')
db = client['myDB']
collection = db['users']
# Try to insert
collection.insert_one({'email': '[email protected]'})
except ConnectionFailure:
print('Failed to connect to MongoDB')
except DuplicateKeyError:
print('Document with this key already exists')
except Exception as e:
print(f'An error occurred: {e}')
finally:
client.close()