Metadata-Version: 2.4
Name: mongoforge
Version: 1.2.0
Summary: Magic filters for MongoDB
Home-page: https://github.com/Fsoky/mongoforge
Author: Fsoky
Author-email: cyberuest0x12@gmail.com
Keywords: mongodb pymongo utils
Description-Content-Type: text/markdown
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: summary

<h1 align="center">MongoForge</h1>
<p align="center">Magic filters for MongoDB</p>

## Installation

```bash
pip install mongoforge
```

## Basic example

```python
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

```python
# Or filter
F.or_f(F.name == "Alex", F.age > 18)
```

```python
# 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

```python
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))
```
