GitHub - bossauh/mongoclass: A basic ORM like interface for mon…

Philippe Mathew De Vera

A basic ORM like interface for mongodb in python that uses dataclasses. The main goal of this library is to simplify the interaction between your python application and your mongodb database.
Mongoclass also supports multiple database engines. By default it's using pymongo, but you can also use mongita for a local database or memory based database (useful for caching).

Getting Started

This section will explain the basics of how to use mongoclass.
from mongoclass import MongoClassClient client = MongoClassClient("mongoclass", "localhost:27018")
This will create a MongoClassClientinstance that exposes the features of mongoclass. MongoClassClient inherits from pymongo.MongoClient so you can also use it like you'd normally use pymongo.

Schemas

To create a schema (or a preferred term, mongoclass), this is all you have to do.
from dataclasses import dataclass @client.mongoclass() @dataclass class User: name: str email: str phone: int country: str = "US"
This creates a User mongoclass that belongs in the user collection inside the default database. To create an actual User object and have it be inserted in the database, create an instance of User and call the .insert() method like so
john = User("John Dee", "johndee@gmail.com", 5821) insert_result = john.insert() # or john.save()
The first line creates the user John Dee with the provided information. Notice how we didn't need to provide a country, that is because country defaults to US.
The second line inserts it to the user collection in the default database and then returns a pymongo.InsertOneResult
As an alternative to having to call .insert(), you can pass _insert=True to User() which will automatically insert as soon as the object is initialized. You do loose the ability to receive the pymongo.InsertOneResult
The entire library has a ton more features that I did not cover here, so for a full documentation, head over to https://oppenheimer.gitbook.io/mongoclass/
Like this project

Posted Oct 8, 2022

A basic ORM like interface for mongodb in python. Contribute to bossauh/mongoclass development by creating an account on GitHub.

A guide to building a offline voice assistant using python
A guide to building a offline voice assistant using python

Join 50k+ companies and 1M+ independents

Contra Logo

© 2025 Contra.Work Inc