Site icon Afzal Badshah, PhD

Printing MongoDB Collection’s Data in Python

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 are new to MongoDB and Python programming.

Printing Collection

Complete Program:

# Import the Required Libraries
import pymongo
from pymongo import MongoClient

# Connect to MongoDB Atlas
client = pymongo.MongoClient("mongodb+srv://****:****@cluster0.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

# Select the Database and Collection
db = client.***
collection = db.***

# Define a Function to Print Collection Data
def print_collection_data():
    try:
        # Retrieve data from MongoDB Atlas
        cursor = collection.find()

        # Print data
        for document in cursor:
            print(document)
    except Exception as e:
        print("An error occurred:", e)

# Call the Function to Print Collection Data
print_collection_data()

Step 1: Import the Required Libraries

import pymongo
from pymongo import MongoClient

Step 2: Connect to MongoDB Atlas

client = pymongo.MongoClient("mongodb+srv://***:***@cluster0.ergtejf.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")

Step 3: Select the Database and Collection

db = client.***
collection = db.***

Step 4: Define a Function to Print Collection Data

def print_collection_data():
    try:
        # Retrieve data from MongoDB Atlas
        cursor = collection.find()

        # Print data
        for document in cursor:
            print(document)
    except Exception as e:
        print("An error occurred:", e)

Step 5: Call the Function to Print Collection Data

print_collection_data()

Printing Selected Columns Data

def print_names():
    try:
        # Retrieve data from MongoDB Atlas with projection for 'name' field
        cursor = collection.find()

        # Print names
        for document in cursor:
            print(document['name'])
    except Exception as e:
        print("An error occurred:", e)

# Call the Function to Print Names from Collection
print_names()

Explanation

Here’s the explanation of each line of the above code:

# Define a Function to Print Names from Collection
def print_names():
    try:
        # Retrieve data from MongoDB Atlas with projection for 'name' field
        cursor = collection.find()
        # Print names
        for document in cursor:
            print(document['name'])
    except Exception as e:
        print("An error occurred:", e)
# Call the Function to Print Names from Collection
print_names()

This code defines a function print_names() to retrieve and print names from a MongoDB collection and then calls this function to execute the retrieval and printing process.

You can read a detailed tutorial related to MongoDB here.

Exit mobile version