![](https://i0.wp.com/afzalbadshah.com/wp-content/uploads/2024/04/MongoDB-jpg.webp?resize=1038%2C576&ssl=1)
Data Manipulation with MongoDB Aggregation Framework in Python
MongoDB Aggregation Framework is a powerful tool that allows for data manipulation and analysis within MongoDB collections. It provides a flexible and efficient way to process and transform data, enabling users to perform complex operations such as grouping, sorting, filtering, and computing aggregate values. In this lab tutorial, we will introduce the concepts of MongoDB Aggregation Framework, provide a detailed explanation of the code, and walk through each line to understand its functionality. Visit the detailed tutorial here.
Code
import pymongo
from pymongo import MongoClient
# Connect to MongoDB
client = pymongo.MongoClient("mongodb+srv://user:pass@cluster0.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")
# Switch to the desired database
db = client.afzal
collection = db.test
# Insert sample data into a collection
db.collection.insert_many([
{ 'name': 'Afzal', 'age': 25, 'city': 'Islamabad' },
{ 'name': 'Jalal', 'age': 28, 'city': 'Mianwali' }
{ 'name': 'Yousaf', 'age': 30, 'city': 'Quetta' },
{ 'name': 'Ibrahim', 'age': 35, 'city': 'Karachi' },
])
print("Total documents in collection:", db.collection.count_documents({}))
# Calculate average age
pipeline_avg_age = [{ '$group': { '_id': None, 'avgAge': { '$avg': '$age' } } }]
avg_age_result = list(db.collection.aggregate(pipeline_avg_age))
print("Average age:", avg_age_result[0]['avgAge'])
# Group by city and count
pipeline_city_count = [{ '$group': { '_id': '$city', 'count': { '$sum': 1 } } }]
city_count_result = list(db.collection.aggregate(pipeline_city_count))
print("City count:", city_count_result)
# Group by city and find max age
pipeline_max_age = [{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]
max_age_result = list(db.collection.aggregate(pipeline_max_age))
print("Max age by city:", max_age_result)
# Filter documents where age is greater than 25
pipeline_filtered = [{ '$match': { 'age': { '$gt': 25 } } }]
filtered_result = list(db.collection.aggregate(pipeline_filtered))
print("Filtered documents:", filtered_result)
# Sort documents by age in descending order
pipeline_sorted = [{ '$sort': { 'age': -1 } }]
sorted_result = list(db.collection.aggregate(pipeline_sorted))
print("Sorted documents:", sorted_result)
Connection and Database Selection
Contents
- We import the necessary libraries
pymongo
andMongoClient
. - We establish a connection to the MongoDB server using the connection string (replace with your actual connection details).
- We specify the desired database (
afzal
) and collection (test
) within the connected client.
import pymongo
from pymongo import MongoClient
# Connect to MongoDB (replace with your connection details)
client = pymongo.MongoClient("mongodb+srv://user:pass@cluster0.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")
# Switch to the desired database and collection (replace with your database and collection names)
db = client.afzal
collection = db.test
Sample Data and Counting Documents
- We insert sample data containing documents with names, ages, and cities.
- We use
db.collection.insert_many
to insert multiple documents at once. - We then count the total number of documents in the collection using
db.collection.count_documents({})
. The empty dictionary{}
specifies matching all documents.
# Insert sample data into a collection
db.collection.insert_many([
{ 'name': 'Afzal', 'age': 25, 'city': 'Islamabad' },
{ 'name': 'Jalal', 'age': 28, 'city': 'Mianwali' }
{ 'name': 'Yousaf', 'age': 30, 'city': 'Quetta' },
{ 'name': 'Ibrahim', 'age': 35, 'city': 'Karachi' },
])
print("Total documents in collection:", db.collection.count_documents({}))
Calculating Average Age
- We define an aggregation pipeline named
pipeline_avg_age
. - The pipeline consists of a single stage using the
$group
operator._id: None
discards the original document’s_id
field in the output.$avg
: This operator calculates the average value of theage
field and stores it in theavgAge
field of the output document.
- We use
db.collection.aggregate(pipeline_avg_age)
to execute the pipeline and retrieve the results. - We convert the results to a list using
list
and access the first element (since there’s only one document in the output) to get the average age stored in theavgAge
field.
# Calculate average age
pipeline_avg_age = [{ '$group': { '_id': None, 'avgAge': { '$avg': '$age' } } }]
avg_age_result = list(db.collection.aggregate(pipeline_avg_age))
print("Average age:", avg_age_result[0]['avgAge'])
Grouping by City and Counting Documents
- The pipeline groups documents by their
city
using$group
. _id: '$city'
sets the group identifier to thecity
field value.$sum: 1
increments a counter for each document in the group, resulting in a count of documents for each city.- We process and print the results similar to the previous step.
# Group by city and find max age
pipeline_max_age = [{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]
max_age_result = list(db.collection.aggregate(pipeline_max_age))
print("Max age by city:", max_age_result)
# Group by city and count
pipeline_city_count = [{ '$group': { '_id': '$city', 'count': { '$sum': 1 } } }]
city_count_result = list(db.collection.aggregate(pipeline_city_count))
print("City count:", city_count_result)
Grouping by City and Finding Maximum Age
- The pipeline groups documents by city using
$group
with_id: '$city'
. $max: '$age'
calculates the maximum value of theage
field within each group, storing it in themaxAge
field of the output document.- We process and print the results as in previous steps.
pipeline_max_age = [{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]
max_age_result = list(db.collection.aggregate(pipeline_max_age))
print("Max age by city:", max_age_result)
Filtering Documents
- We define a pipeline with a single stage using the
$match
operator. $match
filters documents based on a criteria. Here, it selects documents where theage
is greater than ($gt
) 25.- We process and print the results, showcasing the filtered documents.
# Filter documents where age is greater than 25
pipeline_filtered = [{ '$match': { 'age': { '$gt': 25 } } }]
filtered_result = list(db.collection.aggregate(pipeline_filtered))
print("Filtered documents:", filtered_result)
Sorting Documents
- The pipeline uses the
$sort
operator to arrange documents. - Here,
age: -1
sorts documents by theage
field in descending order (highest age first). - We process and print the results, displaying the sorted documents.
# Sort documents by age in descending order
pipeline_sorted = [{ '$sort': { 'age': -1 } }]
sorted_result = list(db.collection.aggregate(pipeline_sorted))
print("Sorted documents:", sorted_result)
30 thoughts on “Data Manipulation with MongoDB Aggregation Framework in Python”
mexican pharmaceuticals online
https://cmqpharma.com/# medicine in mexico pharmacies
buying from online mexican pharmacy
mexico drug stores pharmacies: mexican pharmacy – mexican rx online
canadian drugstore online: canadian pharmacy online store – canadian pharmacy online ship to usa
https://foruspharma.com/# mexican border pharmacies shipping to usa
medication from mexico pharmacy reputable mexican pharmacies online buying from online mexican pharmacy
canadian 24 hour pharmacy: cheapest pharmacy canada – canadapharmacyonline com
canada drugs online review: canadian pharmacy tampa – best canadian pharmacy
best canadian pharmacy canadian pharmacy no scripts buying drugs from canada
northwest pharmacy canada: canadian pharmacy in canada – canadian pharmacy
http://indiapharmast.com/# online pharmacy india
mexican border pharmacies shipping to usa: pharmacies in mexico that ship to usa – п»їbest mexican online pharmacies
mexico pharmacy best online pharmacies in mexico best online pharmacies in mexico
reputable mexican pharmacies online: pharmacies in mexico that ship to usa – buying from online mexican pharmacy
reputable indian pharmacies: top 10 online pharmacy in india – best online pharmacy india
https://foruspharma.com/# mexico pharmacies prescription drugs
indian pharmacies safe: buy medicines online in india – world pharmacy india
india pharmacy mail order pharmacy india top 10 pharmacies in india
п»їbest mexican online pharmacies: mexican drugstore online – mexican online pharmacies prescription drugs
canadian pharmacy store: legitimate canadian pharmacy online – buy prescription drugs from canada cheap
https://ciprodelivery.pro/# antibiotics cipro
http://clomiddelivery.pro/# can i buy cheap clomid without prescription
https://paxloviddelivery.pro/# paxlovid pill
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! However, how could we communicate?
https://paxloviddelivery.pro/# paxlovid india
https://doxycyclinedelivery.pro/# doxycycline 50 mg india
http://paxloviddelivery.pro/# buy paxlovid online
http://paxloviddelivery.pro/# paxlovid buy
https://amoxildelivery.pro/# cost of amoxicillin prescription
https://clomiddelivery.pro/# where to get generic clomid without rx
doxycycline for sale usa: doxycycline over the counter usa – doxycycline capsules purchase