Skip to main content

Build Status

pyjo_mongo

A light wrapper around pymongo and pyjo to easily interact with MongoDB documents. See the following example.

Install

pip install pyjo_mongo

How to use

from pymongo import MongoClient
from pyjo import Model, Field, RangeField, EnumField
from pyjo_mongo import Document

db_connection = MongoClient(MONGODB_URL)[DB_NAME]


class Gender(Enum):
    female = 0
    male = 1


class Address(Model):
    city = Field(type=str)
    postal_code = Field(type=int)
    address = Field()


class User(Document):
    __meta__ = {
        'db_connection': lambda: db_connection,
        'collection_name': 'users',
        'indexes': [
            {
                'fields': ['username'],
                'unique': True,
                'index_background': True,
            },
            ['first_name', 'last_name'],
        ],
    }

    username = Field(type=str, repr=True, required=True)
    first_name = Field(type=str)
    last_name = Field(type=str)
    age = RangeField(min=18, max=120)
    gender = EnumField(enum=Gender)
    address = Field(type=Address)
    active = Field(type=bool, default=True)
u = User(username='mp')
u.id
# None
u.save()
u.id
# ObjectId('5a5ca86080a9b8291874f4db')

# objects is a class property built around a pymongo Cursor
# and will allow you to perform basic operations

u2 = User.objects.find_one({'username': 'mp'})
u2.gender = Gender.male
u2.save()

u.reload()
u.gender
# Gender.male

# queries use the same syntax of pymongo and automatically return pyjo data models
for user in User.objects.find({'active': True}):
    print(user)
# <User(username=mp)>

# You can perform ordering operations on querysets
for user in User.objects.order_by('gender', '-age'):
    print(user.username)
    print(user.age)

Metadata

Release files for pyjo-mongo 0.9.1

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

Source distribution (sdist)

Source distribution for pyjo-mongo 0.9.1
File Size Uploaded
pyjo_mongo-0.9.1.tar.gz 6.4 kB Details

Release files / pyjo_mongo-0.9.1.tar.gz

Download URL pyjo_mongo-0.9.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
50979fb46a5304ac4acf43d2b53c7c78083b1c55fce57827988f60726c9f2bd9
BLAKE2b-256 checksum
How to use checksums
fcbea1077c58a1c0831f5b1a4ccfc6c40a4f576c717b5804c271ba91daf6a1df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.9.1 This release

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.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