0% found this document useful (0 votes)
3 views13 pages

Pym on Go Presentation

This document serves as a comprehensive guide for integrating PyMongo with MongoDB, covering installation, connection setup, and CRUD operations. It includes advanced topics such as error handling, indexing, aggregation pipelines, and working with embedded documents and arrays. Best practices for performance and efficient data management are also highlighted throughout the guide.

Uploaded by

luffysenpai5903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Pym on Go Presentation

This document serves as a comprehensive guide for integrating PyMongo with MongoDB, covering installation, connection setup, and CRUD operations. It includes advanced topics such as error handling, indexing, aggregation pipelines, and working with embedded documents and arrays. Best practices for performance and efficient data management are also highlighted throughout the guide.

Uploaded by

luffysenpai5903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

PyMongo & MongoDB

Integration
Guide with Implementation Details
Introduction to MongoDB & PyMongo

MongoDB: PyMongo:
- NoSQL, document-based. - Official Python driver for MongoDB.
- Stores data in BSON (Binary JSON). - Allows interaction with MongoDB via Python.
- Uses collections of documents instead of rows and tables.
-Cluster → Database → Collection → Documents. Installation:
pip install pymongo
Key Benefits:
- High flexibility and scalability. Basic Import:
- No fixed schema. from pymongo import MongoClient
- Easy replication and sharding.
Connecting to MongoDB Atlas with URI

MongoDB Atlas Setup: PyMongo Connection:


1. Go to https://cloud.mongodb.com/ Code :
2. Create a free cluster. from pymongo import MongoClient
3. Add a database user and password.
4. Whitelist your IP. uri = "your_mongodb_uri"
5. Get connection string (URI). client = MongoClient(uri)
db = client["myDatabase"]
collection = db["myCollection"]

URI Format :
"your_mongodb_uri"= mongodb+srv://<user>:<password>@<cluster>.mongodb.net/<db>?retryWrites=true&w=majority

Example :
"your_mongodb_uri"= mongodb+srv://viratkohli:password@democluster.mongodb.net/demo?retryWrites=true&w=majority
Note : URI format may change over time. You can obtain the current URI or its format during the MongoDB Atlas setup process.
CRUD Operations in PyMongo

Create: Update:
• insert_one() and insert_many() are used to add single • update_one() modifies a single matching document;
or multiple documents to a collection. update_many() applies changes to multiple.
• Automatically assigns a unique _id to each inserted • Uses operators like $set to update and $inc to increment values.
document. Code :
Code : (refer example 1 for complete code) collection.update_one({"name": "Alice"}, {"$set": {"age": 31}})
collection.insert_one({"name": "Alice", "age": 30}) collection.update_many({}, {"$inc": {"age": 1}})
collection.insert_many([{"name": "Bob"}, {"name": "Charlie"}])
Delete:
Read: • delete_one() removes the first matching document;
• find_one() retrieves the first matching document; find() delete_many() removes all that match the condition.
fetches all matching documents. • Useful for data cleanup or removing outdated records.
• Supports powerful query operators like $gte for filtering. Code :
Code : collection.delete_one({"name": "Alice"})
doc = collection.find_one({"name": "Alice"}) collection.delete_many({"age": {"$lt": 20}})
docs = list(collection.find({"age": {"$gte": 25}}))
Advanced Query Operators in PyMongo
Operator Description Example

$gt / $lt Greater / Less than {"age": {"$gt": 30}} (refer Example 2)

$in Match any in list {"status": {"$in": ["A", "B"]}}

$regex Pattern match {"name": {"$regex": "^A"}}

$exists Check if field exists {"email": {"$exists": true}}

$or / $and Logical queries {"$or": [{"age": 25}, {"name": "Bob"}]}}


Error Handling, Debugging & ObjectIds
Common Errors and Handling: ObjectId Handling:
• Handles common PyMongo exceptions like ConnectionFailure • ObjectId is used to query documents by their unique _id field in
and OperationFailure. MongoDB.
• ObjectId("...") converts a string to a valid ObjectId for
• Uses client.server_info() to trigger a connection test.
querying.
• Ensures graceful error handling for connection or authentication
• ObjectId() generates a new unique ID for inserting new
issues. documents.
Code : Code : (refer Example 3)
from pymongo import MongoClient from bson.objectid import ObjectId
from pymongo.errors import ConnectionFailure, OperationFailure document = collection.find_one({"_id":
# MongoDB Atlas URI ObjectId("665d8f1c8f9b3b2c4a43e60e")})
uri="mongodb+srv://viratkohli:password@democluster.mongodb.net/demo? new_id = ObjectId() # Generates a new ObjectId
retryWrites=true&w=majority"
try:
client = MongoClient(uri, serverSelectionTimeoutMS=5000)
client.server_info() # Force connection
except ConnectionFailure:
print("Failed to connect to MongoDB.")
except OperationFailure as e:
print(" Authentication error:", e)
Indexes for Performance & Bulk Operations

Creating Indexes: Bulk Write:


• Improves query performance by indexing specific fields like "email" or • Performs multiple operations (insert, update, delete) in a single request for
combinations like "name" and "age". efficiency.
• unique=True enforces uniqueness on a field to prevent duplicate values. • Reduces network overhead and speeds up batch processing.
Code : (refer Example 4) Code :
from pymongo import MongoClient, InsertOne, UpdateOne, DeleteOne
collection.create_index("email") # Single field
collection.create_index([("name", 1), ("age", -1)]) # Compound index # Connect to MongoDB
collection.create_index("email", unique=True) # Unique index uri =
"mongodb+srv://viratkohli:password@democluster.mongodb.net/demo?retryWrit
Checking Indexes: es=true&w=majority"
• index_information() lists all indexes in a collection with their client = MongoClient(uri)
configuration details. db = client["demo"]
• Useful for verifying which indexes exist and how they are structured. collection = db["users"]
Code : (refer Example 4)
# Define bulk operations
indexes = collection.index_information() ops = [
for name, info in indexes.items(): InsertOne({"name": "John", "age": 35}),
UpdateOne({"name": "Alice"}, {"$set": {"age": 27}}),
print(name, info) DeleteOne({"name": "Charlie"})
]
# Execute bulk write
result = collection.bulk_write(ops)
Aggregation Pipeline in PyMongo

Aggregation Stages: $match, $group, $project, $sort, $limit, $lookup


• Aggregation pipelines process documents through multiple stages for complex queries.
• This example filters active users, groups them by age, sorts by count, and limits results to top 5.
Code :
(refer Example 5)
pipeline = [
{"$match": {"status": "active"}},
{"$group": {"_id": "$age", "count": {"$sum": 1}}},
{"$sort": {"count": -1}},
{"$limit": 5}
]
results = collection.aggregate(pipeline)
for doc in results:
print(doc)
Working with Embedded Documents & Arrays

Arrays:
Embedded Documents:
• Fields can store lists, allowing multiple values like course names in a
• Documents can contain nested sub-documents for structured data (e.g.,
single document.
address inside user).
• Useful for modeling one-to-one relationships directly within a document. Code :
Code : (refer Example 6 ) student = {"name": "Bob", "courses": ["math", "science"]}
user = {"name": "Alice", "address": {"city": "NY", "zip": "10001"}} collection.insert_one(student)
collection.insert_one(user) Query Arrays:
Query Embedded: • Match documents containing specific values, all values, or based on array
length using $all and $size.
Use dot notation (e.g., "address.city") to query fields inside embedded
documents. Code :
Code : collection.find({"courses": "math"})
result = collection.find_one({"address.city": "NY"}) collection.find({"courses": {"$all": ["math", "science"]}})
collection.find({"courses": {"$size": 3}})
Summary & Best Practices

Summary:
- CRUD, Aggregation, Indexing, Transactions, Embedded Docs
- Code examples and URI management
- ObjectId usage and error handling
Best Practices:
- Use indexes wisely
- Validate data before transactions
- Avoid heavy $lookup/$group unless needed
- Use ObjectId for _id operations
- Use bulk_write and pipelines for speed
Examples :
Example 2 :
Example 1 :
from pymongo import MongoClient
from pymongo import MongoClient
# MongoDB Atlas URI
# Connect to MongoDB
uri = uri =
"mongodb+srv://viralkohli:password@democluster.mongodb.net/demo "mongodb+srv://viratkohli:password@democluster.mongodb.net/demo?retryWri
?retryWrites=true&w=majority" tes=true&w=majority"
# Connect to MongoDB client = MongoClient(uri)
client = MongoClient(uri)
# Access the database and collection # Access database and collection
db = client["demo"]
db = client["demo"] # "demo" is the database name
collection = db["users"]
collection = db["users"] # "users" is the collection name
# Sample query: find users where age > 26
# Insert a single document query = {"age": {"$gt": 26}}
collection.insert_one({"name": "Alice", "age": 30}) results = collection.find(query)

# Insert multiple documents # Print results


for doc in results:
collection.insert_many([{"name": "Bob"}, {"name": "Charlie"}]) print(doc)
Examples :
Example 3 : Example 4 :
from pymongo import MongoClient from pymongo import MongoClient
from bson.objectid import ObjectId
# Connect to MongoDB
# Connect to MongoDB
uri =
"mongodb+srv://viratkohli:password@democluster.mongodb. uri =
net/demo?retryWrites=true&w=majority" "mongodb+srv://viratkohli:password@democluster.mongodb.net/dem
client = MongoClient(uri)
o?retryWrites=true&w=majority"
# Access the database and collection
db = client["demo"] client = MongoClient(uri)
collection = db["users"]
db = client["demo"]
try: collection = db["users"]
object_id_str = "665d8f1c8f9b3b2c4a43e60e" # Example _id as
string
document = collection.find_one({"_id": ObjectId(object_id_str)}) # Create indexes
if document: collection.create_index("email") # Single field index
print("Found document:", document) collection.create_index([("name", 1), ("age", -1)]) # Compound index
else:
collection.create_index("email", unique=True) # Unique index on email
print("No document found with _id:", object_id_str)
except Exception as e:
print("Invalid ObjectId or error:", e) # Check and print all indexes
indexes = collection.index_information()
new_id = ObjectId() for name, info in indexes.items():
print("Generated new ObjectId:", new_id)
print(name, info)
Examples
Example 6 :
Example 5 : from pymongo import MongoClient
from pymongo import MongoClient
# Connect to MongoDB
# Connect to MongoDB uri =
"mongodb+srv://viratkohli:password@democluster.mongodb.net/demo?r
uri = etryWrites=true&w=majority"
"mongodb+srv://viratkohli:password@democluster.mongodb.ne client = MongoClient(uri)
t/demo?retryWrites=true&w=majority"
db = client["demo"]
client = MongoClient(uri) collection = db["users"]

db = client["demo"] # Insert a document with an embedded address


collection = db["users"] user = {
"name": "Alice",
"address": {
# Define aggregation pipeline "city": "NY",
pipeline = [ "zip": "10001"
{"$match": {"status": "active"}}, }
{"$group": {"_id": "$age", "count": {"$sum": 1}}}, }
{"$sort": {"count": -1}}, collection.insert_one(user)
{"$limit": 5} # Query the embedded document using dot notation
] result = collection.find_one({"address.city": "NY"})
print(result)
# Run aggregation and print results
results = collection.aggregate(pipeline)
for doc in results:
print(doc)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy