CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-beanie

Asynchronous Python ODM for MongoDB with modern Pydantic-based document mapping

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

bulk-operations.mddocs/

Bulk Operations

Efficient bulk write operations with transaction support for high-performance batch processing of multiple document operations.

Capabilities

BulkWriter

Context manager for efficient bulk write operations with automatic transaction handling.

class BulkWriter:
    """Efficient bulk write operations handler with transaction support."""
    
    def __init__(
        self,
        session: Optional[AsyncClientSession] = None,
        ordered: bool = True,
        object_class: Optional[Type[Union[Document, UnionDoc]]] = None,
        bypass_document_validation: Optional[bool] = False,
        comment: Optional[Any] = None,
    ) -> None:
        """Initialize bulk writer with optional session and configuration."""
        ...
    
    async def __aenter__(self) -> "BulkWriter":
        """Enter context manager."""
        ...
    
    async def __aexit__(self, exc_type, exc_val, exc_tb):
        """Exit context manager and commit operations."""
        ...
    
    def add_operation(self, operation: Dict[str, Any]) -> None:
        """Queue a bulk operation for execution."""
        ...
    
    async def commit(self) -> Any:
        """Execute all queued operations."""
        ...

Usage Examples

from beanie import Document, BulkWriter
from pymongo.asynchronous.client_session import AsyncClientSession
from typing import Optional, Type, Union, Any

class User(Document):
    name: str
    email: str
    
    class Settings:
        collection = "users"

# Basic bulk operations
async def bulk_insert_users():
    users_data = [
        {"name": "Alice", "email": "alice@example.com"},
        {"name": "Bob", "email": "bob@example.com"},
        {"name": "Charlie", "email": "charlie@example.com"}
    ]
    
    async with BulkWriter() as bulk:
        for user_data in users_data:
            user = User(**user_data)
            bulk.add_operation({"insert_one": user})
    
    print("Bulk insert completed")

# Mixed operations
async def bulk_mixed_operations():
    async with BulkWriter() as bulk:
        # Insert new user
        new_user = User(name="David", email="david@example.com")
        bulk.add_operation({"insert_one": new_user})
        
        # Update existing user
        bulk.add_operation({
            "update_one": {
                "filter": {"email": "alice@example.com"},
                "update": {"$set": {"name": "Alice Smith"}}
            }
        })
        
        # Delete user
        bulk.add_operation({
            "delete_one": {
                "filter": {"email": "charlie@example.com"}
            }
        })

Install with Tessl CLI

npx tessl i tessl/pypi-beanie

docs

bulk-operations.md

custom-types.md

documents.md

events-actions.md

fields-types.md

index.md

initialization.md

migrations.md

query-operations.md

time-series.md

tile.json