{"id":3069,"date":"2024-04-03T11:47:47","date_gmt":"2024-04-03T06:47:47","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=3069"},"modified":"2024-04-03T11:47:49","modified_gmt":"2024-04-03T06:47:49","slug":"data-manipulation-with-mongodb-aggregation-framework-in-python","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/","title":{"rendered":"Data Manipulation with MongoDB Aggregation Framework in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.  <a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Visit the detailed tutorial here<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\n\n# Connect to MongoDB\nclient = pymongo.MongoClient(\"mongodb+srv:\/\/user:pass@cluster0.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")\n\n# Switch to the desired database\ndb = client.afzal\ncollection = db.test\n\n# Insert sample data into a collection\ndb.collection.insert_many(&#91;\n    { 'name': 'Afzal', 'age': 25, 'city': 'Islamabad' },\n    { 'name': 'Jalal', 'age': 28, 'city': 'Mianwali' }\n    { 'name': 'Yousaf', 'age': 30, 'city': 'Quetta' },\n    { 'name': 'Ibrahim', 'age': 35, 'city': 'Karachi' },\n])\n\nprint(\"Total documents in collection:\", db.collection.count_documents({}))\n\n# Calculate average age\npipeline_avg_age = &#91;{ '$group': { '_id': None, 'avgAge': { '$avg': '$age' } } }]\navg_age_result = list(db.collection.aggregate(pipeline_avg_age))\nprint(\"Average age:\", avg_age_result&#91;0]&#91;'avgAge'])\n\n# Group by city and count\npipeline_city_count = &#91;{ '$group': { '_id': '$city', 'count': { '$sum': 1 } } }]\ncity_count_result = list(db.collection.aggregate(pipeline_city_count))\nprint(\"City count:\", city_count_result)\n\n# Group by city and find max age\npipeline_max_age = &#91;{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]\nmax_age_result = list(db.collection.aggregate(pipeline_max_age))\nprint(\"Max age by city:\", max_age_result)\n\n# Filter documents where age is greater than 25\npipeline_filtered = &#91;{ '$match': { 'age': { '$gt': 25 } } }]\nfiltered_result = list(db.collection.aggregate(pipeline_filtered))\nprint(\"Filtered documents:\", filtered_result)\n\n# Sort documents by age in descending order\npipeline_sorted = &#91;{ '$sort': { 'age': -1 } }]\nsorted_result = list(db.collection.aggregate(pipeline_sorted))\nprint(\"Sorted documents:\", sorted_result)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Connection and Database Selection<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We import the necessary libraries <code>pymongo<\/code> and <code>MongoClient<\/code>.<\/li>\n\n\n\n<li>We establish a connection to the MongoDB server using the connection string (replace with your actual connection details).<\/li>\n\n\n\n<li>We specify the desired database (<code>afzal<\/code>) and collection (<code>test<\/code>) within the connected client.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\n\n# Connect to MongoDB (replace with your connection details)\nclient = pymongo.MongoClient(\"mongodb+srv:\/\/user:pass@cluster0.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")\n\n# Switch to the desired database and collection (replace with your database and collection names)\ndb = client.afzal\ncollection = db.test<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sample Data and Counting Documents<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We insert sample data containing documents with names, ages, and cities.<\/li>\n\n\n\n<li>We use <code>db.collection.insert_many<\/code> to insert multiple documents at once.<\/li>\n\n\n\n<li>We then count the total number of documents in the collection using <code>db.collection.count_documents({})<\/code>. The empty dictionary <code>{}<\/code> specifies matching all documents.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Insert sample data into a collection\ndb.collection.insert_many(&#91;\n    { 'name': 'Afzal', 'age': 25, 'city': 'Islamabad' },\n    { 'name': 'Jalal', 'age': 28, 'city': 'Mianwali' }\n    { 'name': 'Yousaf', 'age': 30, 'city': 'Quetta' },\n    { 'name': 'Ibrahim', 'age': 35, 'city': 'Karachi' },\n])\n\nprint(\"Total documents in collection:\", db.collection.count_documents({}))\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Calculating Average Age<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We define an aggregation pipeline named <code>pipeline_avg_age<\/code>.<\/li>\n\n\n\n<li>The pipeline consists of a single stage using the <code>$group<\/code> operator.\n<ul class=\"wp-block-list\">\n<li><code>_id: None<\/code> discards the original document&#8217;s <code>_id<\/code> field in the output.<\/li>\n\n\n\n<li><code>$avg<\/code>: This operator calculates the average value of the <code>age<\/code> field and stores it in the <code>avgAge<\/code> field of the output document.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>We use <code>db.collection.aggregate(pipeline_avg_age)<\/code> to execute the pipeline and retrieve the results.<\/li>\n\n\n\n<li>We convert the results to a list using <code>list<\/code> and access the first element (since there&#8217;s only one document in the output) to get the average age stored in the <code>avgAge<\/code> field.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Calculate average age\npipeline_avg_age = &#91;{ '$group': { '_id': None, 'avgAge': { '$avg': '$age' } } }]\navg_age_result = list(db.collection.aggregate(pipeline_avg_age))\nprint(\"Average age:\", avg_age_result&#91;0]&#91;'avgAge'])<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Grouping by City and Counting Documents<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The pipeline groups documents by their <code>city<\/code> using <code>$group<\/code>.<\/li>\n\n\n\n<li><code>_id: '$city'<\/code> sets the group identifier to the <code>city<\/code> field value.<\/li>\n\n\n\n<li><code>$sum: 1<\/code> increments a counter for each document in the group, resulting in a count of documents for each city.<\/li>\n\n\n\n<li>We process and print the results similar to the previous step.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Group by city and find max age\npipeline_max_age = &#91;{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]\nmax_age_result = list(db.collection.aggregate(pipeline_max_age))\nprint(\"Max age by city:\", max_age_result)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Group by city and count\npipeline_city_count = &#91;{ '$group': { '_id': '$city', 'count': { '$sum': 1 } } }]\ncity_count_result = list(db.collection.aggregate(pipeline_city_count))\nprint(\"City count:\", city_count_result)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Grouping by City and Finding Maximum Age<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The pipeline groups documents by city using <code>$group<\/code> with <code>_id: '$city'<\/code>.<\/li>\n\n\n\n<li><code>$max: '$age'<\/code> calculates the maximum value of the <code>age<\/code> field within each group, storing it in the <code>maxAge<\/code> field of the output document.<\/li>\n\n\n\n<li>We process and print the results as in previous steps.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>pipeline_max_age = &#91;{ '$group': { '_id': '$city', 'maxAge': { '$max': '$age' } } }]\nmax_age_result = list(db.collection.aggregate(pipeline_max_age))\nprint(\"Max age by city:\", max_age_result)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Filtering Documents<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We define a pipeline with a single stage using the <code>$match<\/code> operator.<\/li>\n\n\n\n<li><code>$match<\/code> filters documents based on a criteria. Here, it selects documents where the <code>age<\/code> is greater than (<code>$gt<\/code>) 25.<\/li>\n\n\n\n<li>We process and print the results, showcasing the filtered documents.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Filter documents where age is greater than 25\npipeline_filtered = &#91;{ '$match': { 'age': { '$gt': 25 } } }]\nfiltered_result = list(db.collection.aggregate(pipeline_filtered))\nprint(\"Filtered documents:\", filtered_result)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sorting Documents<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The pipeline uses the <code>$sort<\/code> operator to arrange documents.<\/li>\n\n\n\n<li>Here, <code>age: -1<\/code> sorts documents by the <code>age<\/code> field in descending order (highest age first).<\/li>\n\n\n\n<li>We process and print the results, displaying the sorted documents.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Sort documents by age in descending order\npipeline_sorted = &#91;{ '$sort': { 'age': -1 } }]\nsorted_result = list(db.collection.aggregate(pipeline_sorted))\nprint(\"Sorted documents:\", sorted_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 Connection&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":3075,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[489],"tags":[522,562,502],"class_list":["post-3069","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-driven-applications-using-mongodb-python-and-google-colab","tag-mongodb","tag-mongodb-aggregation-framework","tag-python"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Afzal Badshah, PhD\"\/>\n\t<meta name=\"google-site-verification\" content=\"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI\" \/>\n\t<meta name=\"p:domain_verify\" content=\"87b5ca26c36438b20581a102dc290a47\" \/>\n\t<meta name=\"yandex-verification\" content=\"0eb3e2a9157fd34d\" \/>\n\t<meta name=\"keywords\" content=\"mongodb,mongodb aggregation framework,python,data driven applicatons (mongodb, python, google colab)\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2024-04-03T06:47:47+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2024-04-03T06:47:49+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/web.facebook.com\/abmanduri\/\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp\" \/>\n\t\t<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t\t<meta name=\"twitter:data1\" content=\"Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#blogposting\",\"name\":\"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD\",\"headline\":\"Data Manipulation with MongoDB Aggregation Framework in Python\",\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/MongoDB-jpg.webp?fit=1280%2C720&ssl=1\",\"width\":1280,\"height\":720},\"datePublished\":\"2024-04-03T11:47:47+05:00\",\"dateModified\":\"2024-04-03T11:47:49+05:00\",\"inLanguage\":\"en-GB\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#webpage\"},\"articleSection\":\"Data Driven Applicatons (MongoDB, Python, Google Colab), Mongodb, MongoDB Aggregation Framework, python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/afzalbadshah.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"position\":2,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/data-driven-applications-using-mongodb-python-and-google-colab\\\/#listItem\",\"name\":\"Data Driven Applicatons (MongoDB, Python, Google Colab)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/data-driven-applications-using-mongodb-python-and-google-colab\\\/#listItem\",\"position\":3,\"name\":\"Data Driven Applicatons (MongoDB, Python, Google Colab)\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/data-driven-applications-using-mongodb-python-and-google-colab\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#listItem\",\"name\":\"Data Manipulation with MongoDB Aggregation Framework in Python\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#listItem\",\"position\":4,\"name\":\"Data Manipulation with MongoDB Aggregation Framework in Python\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/data-driven-applications-using-mongodb-python-and-google-colab\\\/#listItem\",\"name\":\"Data Driven Applicatons (MongoDB, Python, Google Colab)\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#personImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#authorImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/\",\"name\":\"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD\",\"description\":\"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\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2024\\\/04\\\/MongoDB-jpg.webp?fit=1280%2C720&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#mainImage\",\"width\":1280,\"height\":720},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/04\\\/03\\\/data-manipulation-with-mongodb-aggregation-framework-in-python\\\/#mainImage\"},\"datePublished\":\"2024-04-03T11:47:47+05:00\",\"dateModified\":\"2024-04-03T11:47:49+05:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"alternateName\":\"Afzal Badshah\",\"description\":\"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD","description":"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","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/","robots":"max-image-preview:large","keywords":"mongodb,mongodb aggregation framework,python,data driven applicatons (mongodb, python, google colab)","webmasterTools":{"google-site-verification":"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI","p:domain_verify":"87b5ca26c36438b20581a102dc290a47","yandex-verification":"0eb3e2a9157fd34d","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#blogposting","name":"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD","headline":"Data Manipulation with MongoDB Aggregation Framework in Python","author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp?fit=1280%2C720&ssl=1","width":1280,"height":720},"datePublished":"2024-04-03T11:47:47+05:00","dateModified":"2024-04-03T11:47:49+05:00","inLanguage":"en-GB","mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#webpage"},"articleSection":"Data Driven Applicatons (MongoDB, Python, Google Colab), Mongodb, MongoDB Aggregation Framework, python"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","position":1,"name":"Home","item":"https:\/\/afzalbadshah.com","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","position":2,"name":"Courses","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/#listItem","name":"Data Driven Applicatons (MongoDB, Python, Google Colab)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/#listItem","position":3,"name":"Data Driven Applicatons (MongoDB, Python, Google Colab)","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#listItem","name":"Data Manipulation with MongoDB Aggregation Framework in Python"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#listItem","position":4,"name":"Data Manipulation with MongoDB Aggregation Framework in Python","previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/#listItem","name":"Data Driven Applicatons (MongoDB, Python, Google Colab)"}}]},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/#person","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#personImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author","url":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#authorImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"WebPage","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/","name":"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD","description":"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","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#breadcrumblist"},"author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"creator":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp?fit=1280%2C720&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#mainImage","width":1280,"height":720},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/#mainImage"},"datePublished":"2024-04-03T11:47:47+05:00","dateModified":"2024-04-03T11:47:49+05:00"},{"@type":"WebSite","@id":"https:\/\/afzalbadshah.com\/#website","url":"https:\/\/afzalbadshah.com\/","name":"Afzal Badshah, PhD","alternateName":"Afzal Badshah","description":"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","inLanguage":"en-GB","publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"}}]},"og:locale":"en_GB","og:site_name":"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","og:type":"article","og:title":"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD","og:description":"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","og:url":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp","og:image:width":1280,"og:image:height":720,"article:published_time":"2024-04-03T06:47:47+00:00","article:modified_time":"2024-04-03T06:47:49+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Data Manipulation with MongoDB Aggregation Framework in Python - Afzal Badshah, PhD","twitter:description":"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","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"5 minutes"},"aioseo_meta_data":{"post_id":"3069","title":null,"description":null,"keywords":[],"keyphrases":{"focus":{"keyphrase":"MongoDB Aggregation Framework","score":75,"analysis":{"keyphraseInTitle":{"score":9,"maxScore":9,"error":0},"keyphraseInDescription":{"score":9,"maxScore":9,"error":0},"keyphraseLength":{"score":9,"maxScore":9,"error":0,"length":3},"keyphraseInURL":{"score":5,"maxScore":5,"error":0},"keyphraseInIntroduction":{"score":9,"maxScore":9,"error":0},"keyphraseInSubHeadings":{"score":3,"maxScore":9,"error":1},"keyphraseInImageAlt":[],"keywordDensity":{"score":0,"type":"low","maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":true,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-04-03 05:10:45","updated":"2025-06-10 22:08:58","focus_keyword":"MongoDB Aggregation Framework","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/\" title=\"Courses\">Courses<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/\" title=\"Data Driven Applicatons (MongoDB, Python, Google Colab)\">Data Driven Applicatons (MongoDB, Python, Google Colab)<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tData Manipulation with MongoDB Aggregation Framework in Python\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/afzalbadshah.com"},{"label":"Courses","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/"},{"label":"Data Driven Applicatons (MongoDB, Python, Google Colab)","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/"},{"label":"Data Manipulation with MongoDB Aggregation Framework in Python","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/04\/03\/data-manipulation-with-mongodb-aggregation-framework-in-python\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/04\/MongoDB-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-Nv","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=3069"}],"version-history":[{"count":8,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3069\/revisions"}],"predecessor-version":[{"id":3078,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/3069\/revisions\/3078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/3075"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=3069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=3069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=3069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}