mongo-motors

🌱 Mongo Motors - Async MongoDB Client

MongoDB PyPI Python License

mongo-motors is your go-to Python package for seamless asynchronous MongoDB operations. Built on top of the motor library, it provides a robust and efficient way to manage MongoDB connections, ensuring thread-safe and high-performance interactions with your database.


Source Code Website
github.com/deepmancer/mongo-motors deepmancer.github.io/mongo-motors

✨ Features

πŸ“¦ Installation

Get started quickly by installing mongo-motors using pip:

pip install git+https://github.com/yourusername/mongo-motors.git

πŸ› οΈ Quick Start

πŸ”§ Configuration

Start by creating a configuration object using MongoConfig:

from mongo_motors import MongoConfig

config = MongoConfig(
    host='localhost',
    port=27017,
    db='mydatabase',
)

πŸ—οΈ Creating an AsyncMongo Instance

Next, create an instance of AsyncMongo using your configuration:

from mongo_motors import AsyncMongo

async def main():
    mongo_client = await AsyncMongo.create(config=config)
    print(f"Connected to MongoDB at: {mongo_client.url}")

βš™οΈ Managing MongoDB Sessions

Interact with your MongoDB server using the context manager from get_or_create_session:

from mongo_motors import AsyncMongo

async def main():
    mongo_client = await AsyncMongo.create(config=config)

    async with mongo_client.get_or_create_session() as session:
        # Perform MongoDB operations
        result = await session.collection_name.insert_one({'key': 'value'})
        document = await session.collection_name.find_one({'_id': result.inserted_id})
        print(f"Inserted document: {document}")

    await mongo_client.disconnect()

πŸ” Example Usage

Here’s a complete example to demonstrate how mongo-motors works:

import asyncio
from mongo_motors import AsyncMongo, MongoConfig

async def main():
    config = MongoConfig(
        host='localhost',
        port=27017,
        db='mydatabase',
    )
    client = await AsyncMongo.create(config=config)

    async with client.get_or_create_session() as session:
        # Insert a document
        await session.collection_name.insert_one({'key': 'value'})
        
        # Retrieve the document
        document = await session.collection_name.find_one({'key': 'value'})
        print(f"Document found: {document}")
        
        # Delete documents based on a condition
        keys = [{'key': 'value1'}, {'key': 'value2'}, {'key': 'value3'}]
        for key in keys:
            await session.collection_name.delete_many(key)
        
    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

πŸ›‘οΈ Error Handling

mongo-motors provides custom exceptions to handle various MongoDB-related issues:

πŸ”Œ Disconnecting

Gracefully disconnect from your MongoDB server when you’re done:

await mongo_client.disconnect()

πŸ“„ License

This project is licensed under the Apache License 2.0. For more details, see the LICENSE file.


Supercharge your MongoDB operations with mongo-motors today! 🌱