Skip to main content

A production-ready modern Python MongoDB ODM

Project description

Typed Mongo

A production-ready modern Python MongoDB ODM

In addition to synchronous mode, you can use asynchronous mode, just export from typedmongo.asyncio.

Install

pip install typedmongo

Usage

Usage examples trump all usage documentation. So please look at the Example below first.

Example
from motor.motor_asyncio import AsyncIOMotorClient as MongoClient

import typedmongo.asyncio as mongo


class Wallet(mongo.Table):
    balance: mongo.DecimalField


class User(mongo.MongoTable):
    name: mongo.StringField
    age: mongo.IntegerField
    tags: mongo.ListField[str]
    wallet: mongo.EmbeddedField[Wallet]
    created_at: mongo.DateTimeField = mongo.DateTimeField(
        default=lambda: datetime.datetime.now(datetime.timezone.utc)
    )
    children: mongo.ListField[User]
    extra: mongo.DictField = mongo.DictField(default=dict)


async def main():
    await mongo.initial_collections(
        MongoClient().mongo,
        User,
    )

    # Insert one document
    document_id = await User.objects.insert_one(
        User.load(
            {
                "name": "Aber",
                "age": 18,
                "tags": ["a", "b"],
                "wallet": {"balance": 100},
                "children": [],
            },
        )
    )

    # Find one document
    user = await User.objects.find_one(User._id == document_id, sort=[+User.age])

    # Update one document
    update_result = await User.objects.update_one(
        User._id == document_id, {"$set": {"tags": ["a", "b", "e", "r"]}}
    )

    # Delete one document
    delete_result = await User.objects.delete_one(User._id == document_id)

    # Find one and update
    user = await User.objects.find_one_and_update(
        User._id == document_id, {"$set": {"tags": ["a", "b", "e"]}}
    )

    # Find one and replace
    user = await User.objects.find_one_and_replace(
        User._id == document_id,
        User.load({"name": "Aber", "age": 0}),
        after_document=True,
    )

    # Find one and delete
    user = await User.objects.find_one_and_delete(User._id == document_id)

    # Find many documents and sort
    users = [user async for user in User.objects.find(User.age == 18, sort=[-User.age])]

    # Update many documents
    update_result = await User.objects.update_many(
        User.wallet._.balance == Decimal("100"), {"$inc": {"wallet.balance": 10}}
    )

    # Count documents
    await User.objects.count_documents(User.age >= 0)

    # Bulk write operations
    await User.objects.bulk_write(
        mongo.DeleteOne(User._id == 0),
        mongo.DeleteMany(User.age < 18),
        mongo.InsertOne(User.load({"name": "InsertOne"}, partial=True)),
        mongo.ReplaceOne(User.name == "Aber", User.load({}, partial=True)),
        mongo.UpdateMany({}, {"$set": {"age": 25}}),
        mongo.UpdateMany(User.name == "Yue", {"$set": {"name": "yue"}}),
    )

Table

  • Table.load: Load data from dict to instance, and validate the data.
  • Table.dump: Dump the instance to jsonable dict.

Field

  • ObjectIdField
  • StringField
  • IntegerField
  • DecimalField
  • DateTimeField
  • DictField
  • EmbeddedField
  • ListField

Conditional expressions

Comparison expressions

  • Table.field == value
  • Table.field != value
  • Table.field > value
  • Table.field >= value
  • Table.field < value
  • Table.field <= value

Logical expressions

  • (Table.field == value) & (Table.field == value)
  • (Table.field == value) | (Table.field == value)
  • ~(Table.field == value)
  • ~((Table.field == value) & (Table.field == value))
  • ~((Table.field == value) | (Table.field == value))

Sort expressions

  • +Table.field: Ascending
  • -Table.field: Descending

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

typedmongo-1.4.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

typedmongo-1.4.0-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file typedmongo-1.4.0.tar.gz.

File metadata

  • Download URL: typedmongo-1.4.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for typedmongo-1.4.0.tar.gz
Algorithm Hash digest
SHA256 69434ec7333128c72daee995d5042d8ad9c4231eeddbdd95ba59a193c003be6f
MD5 a8320cda9072e2d3edbc410429ba87d8
BLAKE2b-256 c66bcef3fd8a8386dccad6a699081508559ddf693713bbd069c2bcaa0c750dae

See more details on using hashes here.

File details

Details for the file typedmongo-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: typedmongo-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for typedmongo-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fed56ba3f7b8fb081c953c403372a50d08d274e4a4dd3b7d181da05bd79c6b8
MD5 f58d3fc79523ffef04b07888f1237d3b
BLAKE2b-256 922f49571cc508b2c4bc51d2f4383494f247be319ce0eb07c34bd76c873160a3

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