{"id":2928,"date":"2024-03-20T09:19:46","date_gmt":"2024-03-20T04:19:46","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=2928"},"modified":"2024-03-20T12:00:45","modified_gmt":"2024-03-20T07:00:45","slug":"printing-mongodb-collections-data-in-python","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/","title":{"rendered":"Printing MongoDB Collection&#8217;s Data in Python"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We&#8217;ll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the <code>pymongo<\/code> library. This tutorial is suitable for beginners who are new to MongoDB and Python programming.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Printing Collection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Complete Program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Import the Required Libraries\nimport pymongo\nfrom pymongo import MongoClient\n\n# Connect to MongoDB Atlas\nclient = pymongo.MongoClient(\"mongodb+srv:\/\/****:****@cluster0.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")\n\n# Select the Database and Collection\ndb = client.***\ncollection = db.***\n\n# Define a Function to Print Collection Data\ndef print_collection_data():\n    try:\n        # Retrieve data from MongoDB Atlas\n        cursor = collection.find()\n\n        # Print data\n        for document in cursor:\n            print(document)\n    except Exception as e:\n        print(\"An error occurred:\", e)\n\n# Call the Function to Print Collection Data\nprint_collection_data()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 1: Import the Required Libraries<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We import the <code>pymongo<\/code> library, which allows us to interact with MongoDB from Python.<\/li>\n\n\n\n<li>We specifically import the <code>MongoClient<\/code> class from <code>pymongo<\/code> to establish a connection to the MongoDB server.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 2: Connect to MongoDB Atlas<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client = pymongo.MongoClient(\"mongodb+srv:\/\/***:***@cluster0.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use the <code>MongoClient<\/code> class to establish a connection to the MongoDB Atlas cluster.<\/li>\n\n\n\n<li>The connection string <code>\"mongodb+srv:\/\/...\"<\/code> specifies the MongoDB Atlas URI, including the username, password, cluster address, and other parameters such as <code>retryWrites<\/code> and <code>w<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 3: Select the Database and Collection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db = client.***\ncollection = db.***<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We select the database named *** using the <code>client.<\/code>*** syntax.<\/li>\n\n\n\n<li>Similarly, we select the collection named <code>*** <\/code> within the *** database using the <code>db.***<\/code> syntax.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 4: Define a Function to Print Collection Data<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def print_collection_data():\n    try:\n        # Retrieve data from MongoDB Atlas\n        cursor = collection.find()\n\n        # Print data\n        for document in cursor:\n            print(document)\n    except Exception as e:\n        print(\"An error occurred:\", e)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We define a function named <code>print_collection_data()<\/code> to encapsulate the logic for printing collection data.<\/li>\n\n\n\n<li>Inside the function, we use a <code>try-except<\/code> block to handle exceptions that may occur during data retrieval.<\/li>\n\n\n\n<li>We use the <code>collection.find()<\/code> method to retrieve all documents from the specified collection.<\/li>\n\n\n\n<li>We iterate over the cursor returned by <code>find()<\/code> and print each document to the console.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Step 5: Call the Function to Print Collection Data<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print_collection_data()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We call the <code>print_collection_data()<\/code> function to execute the logic defined within it.<\/li>\n\n\n\n<li>This step triggers the connection to MongoDB Atlas, retrieval of data from the specified collection, and printing of the collection data to the console.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Printing Selected Columns Data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def print_names():\n    try:\n        # Retrieve data from MongoDB Atlas with projection for 'name' field\n        cursor = collection.find()\n\n        # Print names\n        for document in cursor:\n            print(document&#91;'name'])\n    except Exception as e:\n        print(\"An error occurred:\", e)\n\n# Call the Function to Print Names from Collection\nprint_names()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Explanation<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the explanation of each line of the above code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define a Function to Print Names from Collection\ndef print_names():<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We define a Python function named <code>print_names()<\/code> that will be responsible for retrieving and printing names from the MongoDB collection.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    try:<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We begin a try block to handle potential exceptions that may occur during the execution of the code inside the block.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>        # Retrieve data from MongoDB Atlas with projection for 'name' field\n        cursor = collection.find()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We use the <code>find()<\/code> method to retrieve data from the MongoDB collection named <code>collection<\/code>.<\/li>\n\n\n\n<li>We specify an empty filter <code>{}<\/code> as the first parameter to retrieve all documents in the collection.<\/li>\n\n\n\n<li>In the second parameter, we specify the projection to include only the &#8216;name&#8217; field and exclude the &#8216;_id&#8217; field. <code>{ \"_id\": 0, \"name\": 1 }<\/code>.<\/li>\n\n\n\n<li>The result of the <code>find()<\/code> method is assigned to the <code>cursor<\/code> variable, which contains the retrieved documents.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>        # Print names\n        for document in cursor:<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We iterate over each document in the <code>cursor<\/code> object using a for loop.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>            print(document&#91;'name'])<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inside the loop, we print the value of the &#8216;name&#8217; field from each document using dictionary access (<code>document['name']<\/code>).<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    except Exception as e:\n        print(\"An error occurred:\", e)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If any exceptions occur during the execution of the try block, they will be caught here.<\/li>\n\n\n\n<li>We print an error message along with the exception that occurred.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code># Call the Function to Print Names from Collection\nprint_names()<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We call the <code>print_names()<\/code> function to execute the code defined within it and print the names from the MongoDB collection.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This code defines a function <code>print_names()<\/code> to retrieve and print names from a MongoDB collection and then calls this function to execute the retrieval and printing process.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/\" target=\"_blank\" rel=\"noopener\" title=\"\">You can read a detailed tutorial related to MongoDB here. <\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;ll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We&#8217;ll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who are new to MongoDB and Python programming. Printing Collection Complete Program: Step 1: Import the Required Libraries Step 2: Connect to MongoDB Atlas Step 3:&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2938,"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,537,502],"class_list":["post-2928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-driven-applications-using-mongodb-python-and-google-colab","tag-mongodb","tag-printing","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=\"In this tutorial, we&#039;ll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We&#039;ll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who\" \/>\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,printing,python,data driven applicatons (mongodb, python, google colab)\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-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=\"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"In this tutorial, we&#039;ll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We&#039;ll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-jpg.webp\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-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-03-20T04:19:46+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2024-03-20T07:00:45+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=\"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"In this tutorial, we&#039;ll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We&#039;ll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-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=\"4 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\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#blogposting\",\"name\":\"Printing MongoDB Collection\\u2019s Data in Python - Afzal Badshah, PhD\",\"headline\":\"Printing MongoDB Collection&#8217;s Data 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\\\/03\\\/Slide1-6-jpg.webp?fit=1280%2C720&ssl=1\",\"width\":1280,\"height\":720},\"datePublished\":\"2024-03-20T09:19:46+05:00\",\"dateModified\":\"2024-03-20T12:00:45+05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":2,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#webpage\"},\"articleSection\":\"Data Driven Applicatons (MongoDB, Python, Google Colab), Mongodb, Printing, python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-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\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#listItem\",\"name\":\"Printing MongoDB Collection&#8217;s Data 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\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#listItem\",\"position\":4,\"name\":\"Printing MongoDB Collection&#8217;s Data 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\\\/03\\\/20\\\/printing-mongodb-collections-data-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\\\/03\\\/20\\\/printing-mongodb-collections-data-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\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/\",\"name\":\"Printing MongoDB Collection\\u2019s Data in Python - Afzal Badshah, PhD\",\"description\":\"In this tutorial, we'll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We'll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-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\\\/03\\\/Slide1-6-jpg.webp?fit=1280%2C720&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#mainImage\",\"width\":1280,\"height\":720},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2024\\\/03\\\/20\\\/printing-mongodb-collections-data-in-python\\\/#mainImage\"},\"datePublished\":\"2024-03-20T09:19:46+05:00\",\"dateModified\":\"2024-03-20T12:00:45+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":"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD","description":"In this tutorial, we'll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We'll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/","robots":"max-image-preview:large","keywords":"mongodb,printing,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\/03\/20\/printing-mongodb-collections-data-in-python\/#blogposting","name":"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD","headline":"Printing MongoDB Collection&#8217;s Data 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\/03\/Slide1-6-jpg.webp?fit=1280%2C720&ssl=1","width":1280,"height":720},"datePublished":"2024-03-20T09:19:46+05:00","dateModified":"2024-03-20T12:00:45+05:00","inLanguage":"en-GB","commentCount":2,"mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/#webpage"},"articleSection":"Data Driven Applicatons (MongoDB, Python, Google Colab), Mongodb, Printing, python"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-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\/03\/20\/printing-mongodb-collections-data-in-python\/#listItem","name":"Printing MongoDB Collection&#8217;s Data 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\/03\/20\/printing-mongodb-collections-data-in-python\/#listItem","position":4,"name":"Printing MongoDB Collection&#8217;s Data 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\/03\/20\/printing-mongodb-collections-data-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\/03\/20\/printing-mongodb-collections-data-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\/03\/20\/printing-mongodb-collections-data-in-python\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/","name":"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD","description":"In this tutorial, we'll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We'll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-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\/03\/Slide1-6-jpg.webp?fit=1280%2C720&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/#mainImage","width":1280,"height":720},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/#mainImage"},"datePublished":"2024-03-20T09:19:46+05:00","dateModified":"2024-03-20T12:00:45+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":"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD","og:description":"In this tutorial, we'll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We'll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who","og:url":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-jpg.webp","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-jpg.webp","og:image:width":1280,"og:image:height":720,"article:published_time":"2024-03-20T04:19:46+00:00","article:modified_time":"2024-03-20T07:00:45+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Printing MongoDB Collection\u2019s Data in Python - Afzal Badshah, PhD","twitter:description":"In this tutorial, we'll walk through the process of connecting to MongoDB Atlas, a cloud-based MongoDB service, from a Python environment. We'll establish a connection to MongoDB Atlas, select a database and collection, retrieve data from the collection, and print it to the console using the pymongo library. This tutorial is suitable for beginners who","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-jpg.webp","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"4 minutes"},"aioseo_meta_data":{"post_id":"2928","title":null,"description":null,"keywords":[],"keyphrases":{"focus":{"keyphrase":"Printing MongoDB collection","score":44,"analysis":{"keyphraseInTitle":{"score":3,"maxScore":9,"error":1},"keyphraseInDescription":{"score":3,"maxScore":9,"error":1},"keyphraseLength":{"score":9,"maxScore":9,"error":0,"length":3},"keyphraseInURL":{"score":5,"maxScore":5,"error":0},"keyphraseInIntroduction":{"score":3,"maxScore":9,"error":1},"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-03-19 08:37:10","updated":"2025-06-10 22:07:26","focus_keyword":"Printing MongoDB collection","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\tPrinting MongoDB Collection\u2019s Data 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":"Printing MongoDB Collection&#8217;s Data in Python","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/20\/printing-mongodb-collections-data-in-python\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide1-6-jpg.webp?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-Le","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2928","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=2928"}],"version-history":[{"count":10,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2928\/revisions"}],"predecessor-version":[{"id":2959,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2928\/revisions\/2959"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/2938"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=2928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=2928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=2928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}