Skip to main content

MongoForge

Magic filters for MongoDB

Installation

pip install mongoforge

Basic example

from pymongo import MongoClient
from mongoforge import F

cluster = MongoClient(...)
collection = cluster.db.users

if user := collection.find_one(F._id == 1): # {"_id": 1}
    print(user)
else:
    collection.insert_one(...)

Additional filters

# Or filter
F.or_f(F.name == "Alex", F.age > 18)
# In
F.name.in_({"Alex", "Bob"})

Available filters

MongoDB Operator Python Expression Example Output
$eq F.age == 21 {"age": 21}
$gt F.age > 21 {"age": {"$gt": 21}}
$lt F.age < 21 {"age": {"$lt": 21}}
$gte F.age >= 21 {"age": {"$gte": 21}}
$lte F.age <= 21 {"age": {"$lte": 21}}
$or F.or_f(...) {"$or": [{...}]}
$in F.name.in_(...) {"name": {"$in": [...]}

Like ORM

from typing import Annotated
from pydantic import Field

from pymongo import MongoClient

from mongoforge import F, Q
from mongoforge.orm import Model

cluster = MongoClient("mongodb://localhost:27017")


class User(Model):
    id: Annotated[int, Field(..., alias="_id")]
    name: Annotated[str | None, Field(None)]
    balance: Annotated[int, Field(0)]

    class Meta:
        collection = cluster.db.users


user = User.one(F.name == "Alex")
if not user:
    User.insert(_id=1, name="Alex")
else:
    print(user.id, user.name, user.balance)
    user.update(Q(balance__inc=100))

Metadata

Release files for mongoforge 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mongoforge 1.2.0
File Size Uploaded
mongoforge-1.2.0.tar.gz 2.8 kB Details

Release files / mongoforge-1.2.0.tar.gz

Download URL mongoforge-1.2.0.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
35f50f10e821d1c82c1612dfc96a2798267f944fbcb754efb13d385e1742b12f
BLAKE2b-256 checksum
How to use checksums
87dd06dbdd747439af9950c0974d38a286c7cb366555415bfb2ca9d2e1fcbdd5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.0

Release history Release notifications | RSS feed

This release

1.2.0 This release

1 release file

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page