Python MongoDB Collections

Learn how to work with MongoDB collections in Python

šŸ“ Working with Collections

Collections in MongoDB are like tables in SQL databases. They store documents (records) and are created automatically when you insert your first document.


import pymongo

# Connect and access database
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mystore"]

# Access/create collection
customers = db["customers"]
products = db["products"]

print("Collections are ready to use!")
                                    
Auto
Created
Flexible
Schema
Documents
Storage

Collection Concepts

šŸ“„

Document Storage

Collections store JSON-like documents

# Documents in a collection
users = db["users"]
user1 = {"name": "Alice", "age": 25}
user2 = {"name": "Bob", "email": "[email protected]"}
šŸ”„

Dynamic Schema

No fixed structure required

# Different document structures OK
product1 = {"name": "Laptop", "price": 999}
product2 = {"name": "Book", "author": "John"}
šŸ“‹

List Collections

View all collections in database

# List all collections
collections = db.list_collection_names()
print("Collections:", collections)
šŸ—‘ļø

Drop Collections

Delete entire collections

# Drop a collection
db["old_data"].drop()
print("Collection deleted!")

šŸ”Ø Creating Collections

Collections are created automatically when you insert the first document

šŸ”¹ Automatic Collection Creation

import pymongo

# Connect to MongoDB
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["bookstore"]

# Access collection (not created yet)
books = db["books"]
print(f"Collection object: {books.name}")

# Check if collection exists (will be False)
collection_exists = "books" in db.list_collection_names()
print(f"Collection exists: {collection_exists}")  # False

# Insert first document - this creates the collection
first_book = {
    "title": "Python Programming",
    "author": "John Smith",
    "price": 29.99,
    "pages": 350
}

result = books.insert_one(first_book)
print(f"Document inserted with ID: {result.inserted_id}")

# Now collection exists
collection_exists = "books" in db.list_collection_names()
print(f"Collection exists after insert: {collection_exists}")  # True

šŸ”¹ Creating Multiple Collections

# Create multiple collections by inserting data
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["library"]

# Create books collection
books = db["books"]
books.insert_one({"title": "Python Guide", "author": "Alice"})

# Create authors collection
authors = db["authors"]
authors.insert_one({"name": "Alice Johnson", "country": "USA"})

# Create members collection
members = db["members"]
members.insert_one({"name": "Bob Smith", "membership_id": 1001})

# List all collections
collections = db.list_collection_names()
print(f"Created collections: {collections}")

# Count documents in each collection
for collection_name in collections:
    count = db[collection_name].count_documents({})
    print(f"{collection_name}: {count} documents")

šŸ“ Collection Naming Rules

MongoDB has specific rules for collection names

āœ… Valid Collection Names

  • Can contain letters, numbers, and underscores
  • Cannot start with "system." (reserved)
  • Cannot contain $ symbol
  • Maximum 120 characters

āŒ Invalid Names

  • Empty string ""
  • Names starting with "system."
  • Names containing $ symbol
# Good collection names
good_names = [
                "users",
                "products",
                "order_items",
                "user_profiles",
                "blog_posts",
                "categories2024"
            ]

# Bad collection names (avoid these)
bad_names = [
    "",              # Empty string
    "system.users",  # Starts with system.
    "user$data",     # Contains $ symbol
    "my collection"  # Contains space (technically allowed but not recommended)
]

# Create collections with good names
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["teststore"]

for name in good_names:
    collection = db[name]
    # Insert dummy data to create collection
    collection.insert_one({"test": True})
    print(f"āœ… Created collection: {name}")

print(f"Total collections: {len(db.list_collection_names())}")

šŸ“‹ Listing Collections

View and manage existing collections

šŸ”¹ List All Collections

import pymongo

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mystore"]

# Get list of collection names
collection_names = db.list_collection_names()
print("Available collections:")
for name in collection_names:
    print(f"  - {name}")

# Get detailed collection information
collections_info = db.list_collections()
print("\nDetailed collection info:")
for collection in collections_info:
    print(f"Name: {collection['name']}")
    print(f"Type: {collection['type']}")
    print("---")

šŸ”¹ Check Collection Existence

# Function to check if collection exists
def collection_exists(db, collection_name):
    return collection_name in db.list_collection_names()

# Check multiple collections
collections_to_check = ["users", "products", "orders", "nonexistent"]

for collection_name in collections_to_check:
    exists = collection_exists(db, collection_name)
    status = "āœ… EXISTS" if exists else "āŒ NOT FOUND"
    print(f"Collection '{collection_name}': {status}")

# Get collection statistics
def get_collection_stats(db, collection_name):
    if collection_exists(db, collection_name):
        collection = db[collection_name]
        count = collection.count_documents({})
        return {
            "name": collection_name,
            "document_count": count,
            "exists": True
        }
    else:
        return {
            "name": collection_name,
            "exists": False
        }

# Show stats for all collections
for name in ["users", "products", "orders"]:
    stats = get_collection_stats(db, name)
    if stats["exists"]:
        print(f"{stats['name']}: {stats['document_count']} documents")
    else:
        print(f"{stats['name']}: Collection not found")

āš™ļø Collection Operations

Common operations you can perform on collections

šŸ”¹ Collection Information

# Get collection information
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["mystore"]
users = db["users"]

# Insert some sample data first
sample_users = [
    {"name": "Alice", "age": 25, "city": "New York"},
    {"name": "Bob", "age": 30, "city": "London"},
    {"name": "Charlie", "age": 35, "city": "Tokyo"}
]
users.insert_many(sample_users)

# Get collection statistics
print(f"Collection name: {users.name}")
print(f"Database name: {users.database.name}")
print(f"Full name: {users.full_name}")

# Count documents
total_docs = users.count_documents({})
print(f"Total documents: {total_docs}")

# Count with filter
adults = users.count_documents({"age": {"$gte": 18}})
print(f"Adults (18+): {adults}")

# Estimated document count (faster for large collections)
estimated_count = users.estimated_document_count()
print(f"Estimated count: {estimated_count}")

šŸ”¹ Dropping Collections

# Drop (delete) collections
client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client["teststore"]

# Create a temporary collection
temp_collection = db["temporary_data"]
temp_collection.insert_one({"test": "data"})

print("Before drop:")
print(f"Collections: {db.list_collection_names()}")

# Drop the collection
temp_collection.drop()

print("After drop:")
print(f"Collections: {db.list_collection_names()}")

# Safe drop function
def safe_drop_collection(db, collection_name):
    if collection_name in db.list_collection_names():
        db[collection_name].drop()
        print(f"āœ… Dropped collection: {collection_name}")
    else:
        print(f"ā„¹ļø Collection '{collection_name}' doesn't exist")

# Test safe drop
safe_drop_collection(db, "nonexistent")
safe_drop_collection(db, "users")

šŸŽÆ Practical Example: Blog System

Create a complete collection structure for a blog system

import pymongo
from datetime import datetime

def setup_blog_collections():
    # Connect to MongoDB
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    db = client["blog_system"]
    
    # 1. Posts collection
    posts = db["posts"]
    sample_posts = [
        {
            "title": "Getting Started with MongoDB",
            "content": "MongoDB is a great NoSQL database...",
            "author": "john_doe",
            "created_at": datetime.now(),
            "tags": ["mongodb", "database", "tutorial"],
            "published": True
        },
        {
            "title": "Python Tips and Tricks",
            "content": "Here are some useful Python tips...",
            "author": "jane_smith",
            "created_at": datetime.now(),
            "tags": ["python", "programming", "tips"],
            "published": False
        }
    ]
    posts.insert_many(sample_posts)
    
    # 2. Authors collection
    authors = db["authors"]
    sample_authors = [
        {
            "username": "john_doe",
            "name": "John Doe",
            "email": "[email protected]",
            "bio": "Tech writer and developer",
            "joined": datetime.now()
        },
        {
            "username": "jane_smith",
            "name": "Jane Smith",
            "email": "[email protected]",
            "bio": "Python enthusiast",
            "joined": datetime.now()
        }
    ]
    authors.insert_many(sample_authors)
    
    # 3. Comments collection
    comments = db["comments"]
    sample_comments = [
        {
            "post_id": posts.find_one({"title": "Getting Started with MongoDB"})["_id"],
            "author": "reader1",
            "content": "Great tutorial!",
            "created_at": datetime.now()
        }
    ]
    comments.insert_many(sample_comments)
    
    # 4. Categories collection
    categories = db["categories"]
    sample_categories = [
        {"name": "Technology", "description": "Tech-related posts"},
        {"name": "Programming", "description": "Programming tutorials"},
        {"name": "Database", "description": "Database topics"}
    ]
    categories.insert_many(sample_categories)
    
    # Show results
    collections = db.list_collection_names()
    print("āœ… Blog system collections created:")
    
    for collection_name in collections:
        count = db[collection_name].count_documents({})
        print(f"šŸ“ {collection_name}: {count} documents")
    
    return db

# Setup the blog system
blog_db = setup_blog_collections()

# Demonstrate collection operations
print("\nšŸ” Collection Operations:")

# Find a post
posts = blog_db["posts"]
first_post = posts.find_one({"published": True})
print(f"First published post: {first_post['title']}")

# Count by category
tech_posts = posts.count_documents({"tags": "mongodb"})
print(f"MongoDB posts: {tech_posts}")

# List all authors
authors = blog_db["authors"]
for author in authors.find({}, {"name": 1, "username": 1}):
    print(f"Author: {author['name']} (@{author['username']})")

🧠 Test Your Knowledge

When is a MongoDB collection actually created?

Which collection name is INVALID?

How do you delete a collection in MongoDB?