Python with MongoDB
Python with MongoDB
Python
interacts with MongoDB using the pymongo library, which allows you to perform
various database operations like creating databases, inserting documents,
querying data, and more.
MongoDB
MongoDB
is a NoSQL database that stores data in flexible, JSON-like documents (BSON).
It is known for its scalability and flexibility, which makes it a popular
choice for handling large amounts of unstructured data.
SQL vs NoSQL
- SQL
(Structured Query Language):
- Relational
databases (e.g., MySQL, PostgreSQL).
- Data
is stored in tables with fixed schemas.
- Uses
SQL for querying and managing data.
- Ideal
for applications with complex transactions and relationships.
- NoSQL
(Not Only SQL):
- Non-relational
databases (e.g., MongoDB).
- Data
is stored in flexible, JSON-like documents or key-value pairs.
- Offers
high flexibility and scalability.
- Suitable
for applications with large volumes of unstructured data or where schema
evolves over time.
Getting Connection String (MongoDB URI)
A
connection string (or URI) is required to connect to a MongoDB instance. For
MongoDB Atlas, it looks like this:
Php
mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
- <username>:
Your MongoDB username.
- <password>:
Your MongoDB password.
- cluster0.mongodb.net:
The hostname of your MongoDB cluster.
- <dbname>:
The database you want to connect to.
Connecting Django Application to
MongoDB Cluster
To
connect a Django application to a MongoDB cluster, you'll need the djongo
package, which allows Django to interact with MongoDB as if it were a
relational database.
- Install
Dependencies:
Bash
pip
install django djongo
- Configure
Django Settings:
Modify the settings.py
file in your Django project to use MongoDB as your database:
Python
DATABASES
= {
'default': {
'ENGINE': 'djongo',
'NAME': '<dbname>',
'ENFORCE_SCHEMA': False,
'CLIENT': {
'host':
'mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>',
'authSource': 'admin',
'retryWrites': True,
'w': 'majority',
}
}
}
Replace <username>,
<password>,
and <dbname>
with your MongoDB credentials and database name.
Creating a Database and Collection
In
MongoDB, databases and collections are created automatically when you first
store data in them. However, you can explicitly create them if needed.
Python
from
pymongo import MongoClient
client
=
MongoClient('mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority')
db
= client['mydatabase'] # Create or
access a database
collection
= db['mycollection'] # Create or access
a collection
Inserting Many Documents to Collection
To
insert multiple documents into a collection:
Python
documents
= [
{'name': 'Alice', 'age': 25},
{'name': 'Bob', 'age': 30},
{'name': 'Charlie', 'age': 35}
]
collection.insert_many(documents)
MongoDB Find
To
retrieve documents from a collection:
Python
for
doc in collection.find():
print(doc)
Find with Query
To
find documents that match specific criteria:
Python
query
= {'age': {'$gt': 30}} # Find documents
where age is greater than 30
for
doc in collection.find(query):
print(doc)
Find Query with Modifier
Using
query modifiers like $or
to refine search results:
Python
query
= {'$or': [{'age': 25}, {'name': 'Charlie'}]}
for
doc in collection.find(query):
print(doc)
Limiting Documents
To
limit the number of documents returned by a query:
Python
for
doc in collection.find().limit(2):
print(doc)
Find with Sort
To
sort documents by a specific field:
Python
for
doc in collection.find().sort('age', 1):
# 1 for ascending, -1 for descending
print(doc)
Update with Query
To
update documents matching a query:
Python
query
= {'name': 'Bob'}
new_values
= {'$set': {'age': 31}}
collection.update_one(query,
new_values)
Delete Document
To
delete a document:
Python
query
= {'name': 'Alice'}
collection.delete_one(query)
Drop a Collection
To
drop (delete) an entire collection:
Python
collection.drop()
Summary
- Python
with MongoDB:
Use pymongo
to interact with MongoDB in Python.
- MongoDB:
A NoSQL database using flexible, JSON-like documents.
- SQL
vs NoSQL:
SQL databases use fixed schemas and tables; NoSQL databases (like MongoDB)
offer flexibility and scalability with JSON-like documents.
- Connection
String:
URI format to connect to MongoDB, e.g., mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority.
- Connecting
Django:
Use djongo
to connect Django to MongoDB; configure it in settings.py.
- Creating
Database/Collection: Automatically created upon first
use but can be explicitly created.
- Inserting
Documents:
Use insert_many
to add multiple documents.
- Finding
Documents:
Use find
for retrieval, with queries to filter results.
- Limiting
and Sorting:
Use limit
to restrict results and sort to order them.
- Updating/Deleting:
Use update_one
to modify and delete_one
to remove documents.
- Dropping
Collection:
Use drop
to delete a collection entirely.
Module 1: Introduction to Python
Overview of Python
Python
is a high-level, interpreted programming language known for its simplicity and
readability. It is used in various fields, from web development to data
science.
History and Features
- Created
by:
Guido van Rossum, first released in 1991.
- Features:
- Readable
syntax
- Extensive
standard libraries
- Supports
multiple programming paradigms (procedural, object-oriented, functional)
Setting Up the Environment
- Download
Python:
Install from python.org.
- Install
an IDE:
Popular choices include PyCharm, VS Code, and Jupyter Notebook.
Basic Syntax
Python
#
Hello World Example
print("Hello,
World!")
Data Types and Variables
- Common
Data Types:
Integer, Float, String, Boolean
Python
age
= 30 # Integer
height
= 5.9 # Float
name
= "Alice" # String
is_student
= True # Boolean
Control Structures
- If
Statement:
Python
if
age > 18:
print("Adult")
else:
print("Minor")
- Loops:
- For
Loop:
Python
for
i in range(5):
print(i)
- While
Loop:
Python
count
= 0
while
count < 5:
print(count)
count += 1
Functions and Modules
- Defining
a Function:
Python
def
greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
- Importing
Modules:
Python
import
math
print(math.sqrt(16)) # Output: 4.0
Data Structures
- Lists:
Ordered, mutable collections.
Python
fruits
= ["apple", "banana", "cherry"]
- Tuples:
Ordered, immutable collections.
Python
coordinates
= (10, 20)
- Sets:
Unordered collections of unique elements.
Python
unique_numbers
= {1, 2, 3, 3}
- Dictionaries:
Key-value pairs.
Python
person
= {"name": "Alice", "age": 25}
Module 2: Introduction to MongoDB
What is MongoDB?
MongoDB
is a NoSQL database designed for scalability and flexibility, allowing
unstructured data storage.
NoSQL Databases vs. SQL Databases
- NoSQL:
Schema-less, horizontal scaling (e.g., MongoDB).
- SQL:
Structured, predefined schema, vertical scaling (e.g., MySQL).
MongoDB Architecture
- Documents:
JSON-like format (BSON).
- Collections:
Groups of documents, similar to tables in SQL.
MongoDB Atlas
A
cloud database service for MongoDB.
Creating an Atlas Account
- Sign
up at MongoDB Atlas.
- Verify
your email and log in.
Setting Up a Cluster
- Create
a new cluster in the Atlas dashboard.
- Choose
your cloud provider and region.
Database and User Management
- Create
a database:
In the cluster, go to "Database" tab and create a new database.
- User
Management:
Create users with specific roles (read, write).
Module 3: Connecting Python to MongoDB
Installing Required Libraries
Use pip
to install pymongo.
Bash
pip
install pymongo
Connecting to MongoDB Atlas
- Connection
String Format:
Php
mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority
- Using
MongoClient in Python:
Python
from
pymongo import MongoClient
client
= MongoClient("your_connection_string")
db
= client['your_database_name']
Module 4: MongoDB CRUD Operations
Creating a Database and Collection
- Database
Creation:
Python
db
= client['test_db'] # Automatically
created when a document is added
- Collection
Creation:
Python
collection
= db['users'] # Automatically created
when a document is added
Inserting Documents
- Using
insert_one:
Python
user
= {"name": "Alice", "age": 30}
collection.insert_one(user)
- Using
insert_many:
Python
users
= [{"name": "Bob", "age": 25}, {"name":
"Charlie", "age": 35}]
collection.insert_many(users)
Reading Documents
- Querying
with find_one:
Python
result
= collection.find_one({"name": "Alice"})
print(result)
- Filtering
Results:
Python
all_users
= collection.find({"age": {"$gt": 25}})
for
user in all_users:
print(user)
Updating Documents
- Using
update_one:
Python
collection.update_one({"name":
"Alice"}, {"$set": {"age": 31}})
- Using
update_many:
Python
collection.update_many({"age":
{"$lt": 30}}, {"$set": {"status":
"young"}})
Deleting Documents
- Using
delete_one:
Python
collection.delete_one({"name":
"Bob"})
- Using
delete_many:
Python
collection.delete_many({"age":
{"$lt": 30}})
Module 5: Querying and Indexing
Query Operators
- Comparison
Operators:
$gt,
$lt,
$eq,
etc.
- Logical
Operators:
$and,
$or,
$not.
Regular Expressions in Queries
Python
results
= collection.find({"name": {"$regex":
"^A"}}) # Names starting with
'A'
Indexing in MongoDB
- Importance
of Indexing:
Improves query performance.
- Creating
an Index:
Python
collection.create_index([("name",
1)]) # Ascending index
- Performance
Considerations:
Indexes speed up reads but slow down writes.
Module 6: Advanced MongoDB Concepts
Aggregation Framework
- Introduction
to Aggregation:
Perform operations on a collection of documents to get aggregated results.
- Using
aggregate:
Python
pipeline
= [
{"$match": {"age":
{"$gt": 25}}},
{"$group": {"_id":
"$age", "count": {"$sum": 1}}}
]
result
= collection.aggregate(pipeline)
for
doc in result:
print(doc)
Data Modeling
- Schema
Design Considerations: Choose between embedding and
referencing data.
- Normalization
vs. Denormalization: Balance between flexibility and
redundancy.
Transactions
- Understanding
Multi-Document Transactions: Execute
multiple operations atomically.
- Implementing
Transactions:
Python
with
client.start_session() as session:
with session.start_transaction():
collection.insert_one({"name": "David"},
session=session)
Module 7: Using Python Libraries with
MongoDB
Flask/Django Integration
- Setting
Up a Web Application: Use Flask or Django to create a
web app.
- Connecting
to MongoDB:
Python
from
flask import Flask
from
pymongo import MongoClient
app
= Flask(__name__)
client
= MongoClient("your_connection_string")
db
= client['your_database_name']
Data Visualization
- Using
Libraries:
Libraries like Matplotlib and Seaborn for visualizing data.
Python
import
matplotlib.pyplot as plt
data
= [1, 2, 3, 4]
plt.plot(data)
plt.show()
- Visualizing
Data Fetched from MongoDB: Plotting user
ages, for example.
Module 8: Deployment and Best Practices
Deployment Strategies
- Hosting
Options:
Use cloud providers like Heroku, AWS, or DigitalOcean for hosting Python
apps with MongoDB.
Backup and Restore
- Backup
Strategies:
Regular backups using MongoDB tools or cloud services.
Security Best Practices
- Authentication
and Authorization: Use role-based access controls.
- Securing
Connections and Data: Use TLS/SSL for connections.
Module 9: Project Work
Capstone Project
- Building
a Complete Application: Implement a web application
using Python and MongoDB.
- Implementing
CRUD Operations:
Create, read, update, and delete functionalities.
- User
Authentication and Data Visualization: Incorporate
user management and visual insights.
Additional Resources
- Recommended
Books:
"Python Crash Course," "Fluent Python."
- Online
Courses:
Platforms like Coursera, Udacity, or freeCodeCamp.
- Community
Forums:
Stack Overflow, Reddit for troubleshooting and community support.
Summary
- Introduction
to Python:
- Overview:
High-level, readable language.
- Features:
Simple syntax, rich libraries, multiple paradigms.
- Basics:
Variables, data types (lists, tuples, sets, dictionaries), control
structures (if statements, loops), and functions.
- Introduction
to MongoDB:
- NoSQL
database designed for flexibility and scalability.
- Differences
between NoSQL and SQL databases.
- MongoDB
Atlas for cloud database management.
- Connecting
Python to MongoDB:
- Install
pymongo
to connect.
- Use
connection strings to access MongoDB Atlas.
- CRUD
Operations:
- Create,
Read, Update, Delete operations on databases and collections using insert_one,
find,
update_one,
and delete_one.
- Querying
and Indexing:
- Use
query operators and regular expressions for filtering.
- Importance
and creation of indexes for performance.
- Advanced
Concepts:
- Aggregation
framework for complex queries.
- Data
modeling considerations (normalization vs. denormalization).
- Transactions
for atomic operations.
- Using
Python Libraries:
- Integration
with web frameworks like Flask and Django.
- Data
visualization with libraries like Matplotlib.
- Deployment
and Best Practices:
- Hosting
strategies for Python applications.
- Backup
and restore strategies, along with security best practices.
- Project
Work:
- Capstone
project building a full application with user authentication and data
visualization.
No comments:
Post a Comment