Skip to main content

An asynchronous Python package for streamlined collection management in MongoDB

Project description

Simple-MongoDB

Supported-Python-Versions-Badge

Documentation-Badge Source-Code-Badge Changelog-Badge

License-Badge PyPi-Download-Badge Package-version-Badge Build-Action-Badge

Created-Badge Last-Commit-Badge

Warning: This Python package is currently still in development phase

Description

Placeholder

Installation

pip install simple-mongodb

Simple Example

import asyncio
from typing import Any

from bson import ObjectId
from pydantic import BaseModel

from simple_mongodb import BaseCollection, MongoDBClient


class AccountCollection(BaseCollection):
    db = 'my-db'  # The name of the database or set the enviroment variable MONGODB_DB
    collection = 'account-collection'  # The name of the collection


class Account(BaseModel):
    name: str


async def main() -> None:
    # Initialize a client object and pass the url or set enviroment variables
    #   MONGODB_HOST, MONGODB_PORT,
    #   MONGODB_USERNAME, MONGODB_PASSWORD
    # Is the url param or enviroment variables not set the default values are used
    client: MongoDBClient = MongoDBClient(url='mongodb://user:pass@host:27017')

    # Initialize the account collection
    account_collection: AccountCollection = AccountCollection(client=client)

    account: Account = Account(name='example-name')

    try:

        # Insert the document in the collection
        document: dict[str, Any] = account.model_dump()
        inserted_id: ObjectId = await account_collection.insert_one(document=document)

        # Find the document
        where: dict[str, Any] = {'_id': inserted_id}
        document: dict[str, Any] = await account_collection.find_one(where=where)

        # Update the document
        update: dict[str, Any] = {'$set': {'name': 'other-name'}}
        # Returns the id of the new document if upsert=True
        await account_collection.update_one(where=where, update=update, upsert=False)

    except account_collection.InsertError:
        pass
    except account_collection.FindError:
        pass
    except account_collection.UpdateError:
        pass
    except account_collection.ServerTimeoutError:
        pass

    # Close the db connection
    client.close()


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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_mongodb-0.8.4.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

simple_mongodb-0.8.4-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file simple_mongodb-0.8.4.tar.gz.

File metadata

  • Download URL: simple_mongodb-0.8.4.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for simple_mongodb-0.8.4.tar.gz
Algorithm Hash digest
SHA256 5220b7969cae9725d8728a5efee92b766bf9c3962ad2ea7c8a58d6842345a0a5
MD5 dbac4f92086f158b0d580f80212a5408
BLAKE2b-256 e3ca2e13fc4c499bd398974de38dfc9dd5482c03ad7924e5a65c004abfb3a8d1

See more details on using hashes here.

File details

Details for the file simple_mongodb-0.8.4-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_mongodb-0.8.4-py3-none-any.whl
Algorithm Hash digest
SHA256 087fccdef56400f2c4b0580cd20bd75b3411da05b9f4d8528bde66cba9c4f872
MD5 28fca59638818a85cee9933998769da0
BLAKE2b-256 a8f8fce4a1342f3a75f8ce6bb2e48191b956f19a9a46cd1799d5d45c264b4c45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page