|
import os |
|
from pymongo import MongoClient |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
atlas_uri = os.getenv("ATLAS_URI") |
|
client = MongoClient(atlas_uri) |
|
|
|
def list_all_collections(): |
|
"""List all databases and their collections in the Atlas cluster""" |
|
try: |
|
|
|
db_names = client.list_database_names() |
|
|
|
print("\nDatabases and Collections in your Atlas cluster:\n") |
|
|
|
|
|
for db_name in db_names: |
|
print(f"Database: {db_name}") |
|
db = client[db_name] |
|
collections = db.list_collection_names() |
|
|
|
for collection in collections: |
|
|
|
count = db[collection].count_documents({}) |
|
print(f" βββ Collection: {collection} ({count} documents)") |
|
print() |
|
|
|
except Exception as e: |
|
print(f"Error: {str(e)}") |
|
finally: |
|
client.close() |
|
|
|
if __name__ == "__main__": |
|
list_all_collections() |
|
|