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.9.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_mongodb-0.9.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_mongodb-0.9.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for simple_mongodb-0.9.0.tar.gz
Algorithm Hash digest
SHA256 4533e9687583ddbff884a64b4812535fa15017fb76eba73b2dabbc50ec16c90d
MD5 ab794ad1235d09e7223ef8446e95ca98
BLAKE2b-256 83196772a21a6981590fd012d547caf655c1e42479117a4197fe58a153bfbfbb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_mongodb-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for simple_mongodb-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 58e97bf728085c3fb69ca5f53b37c88d3e75247c69cf2e9c20523be4a633fd97
MD5 fadc52e239ff32eb16079b095d7258e2
BLAKE2b-256 d383d195c65c331b0e51de45ad2928ca915c83026347e536567cf3e0222c3ea4

See more details on using hashes here.

Supported by

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