MongoDB-ODM
MongoDB-ODM, NOSQL databases in Python, designed for simplicity, compatibility, and robustness.
Documentation: https://mongodb-odm.readthedocs.io
PyPi: https://pypi.org/project/mongodb-odm
Repository: https://github.com/nayan32biswas/mongodb-odm
Introduction
The purpose of this module is to provide easy access to the database with the python object feature with MongoDB and PyMongo. With PyMongo that was very easy to make spelling mistakes in a collection name when you are doing database operation. This module provides you with minimal ODM with a modeling feature so that you don’t have to look up the MongoDB dashboard(Mongo Compass) to know about field names or data types.
MongoDB-ODM is based on Python type annotations, and powered by PyMongo and Pydantic.
The key features are:
- Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
- Easy to use: It has sensible defaults and does a lot of work underneath to simplify the code you write.
- Compatible: It is designed to be compatible with FastAPI, Pydantic, and PyMongo.
- Extensible: You have all the power of PyMongo and Pydantic underneath.
- Short: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in PyMongo and Pydantic.
Requirements
MongoDB-ODM will work on Python 3.9 and above.
MongoDB-ODM is built on top of PyMongo and Pydantic. These packages are required and will be auto-installed when MongoDB-ODM is installed.
Installation
$ pip install mongodb-odm
Example
Define model
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("rating", ASCENDING)]),
]
Set Connection
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
Create Document
pele = Player(name="Pelé", country_code="BRA").create()
maradona = Player(name="Diego Maradona", country_code="ARG", rating=97).create()
zidane = Player(name="Zinedine Zidane", country_code="FRA", rating=96).create()
Retrieve Document
Find data from collection
for player in Player.find():
print(player)
Find one object with filter
player = Player.find_one({Player.name: "Pelé"})
Update Data
player = Player.find_one({Player.name: "Pelé"})
if player:
player.rating = 98 # potential
player.update()
Delete Data
player = Player.find_one({Player.name: "Pelé"})
if player:
player.delete()
Apply Indexes
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("rating", ASCENDING)]),
]
- To create indexes in the database, declare an IndexModel and assign it to the indexes array in the ODMConfig class. IndexModel modules are directly imported from PyMongo.
- Import
apply_indexesfrommongodb_odm. Call theapply_indexesfunction from your CLI. You can use Typer to implement a CLI.
Example Code
This is the example of full code of above.
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("rating", ASCENDING)]),
]
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
pele = Player(name="Pelé", country_code="BRA").create()
maradona = Player(name="Diego Maradona", country_code="ARG", rating=97).create()
zidane = Player(name="Zinedine Zidane", country_code="FRA", rating=96).create()
for player in Player.find():
print(player)
player = Player.find_one({Player.name: "Pelé"})
if player:
player.rating = 98 # potential
player.update()
player = Player.find_one({Player.name: "Pelé"})
if player:
player.delete() # RIP
Supported Framework
MongoDB-ODM is not framework-dependent. You can use this package in any system. However, we take special consideration to be compatible with FastAPI and Flask.
Credit
This package is built on top of PyMongo and Pydantic.
Documentation generated by MkDocs and Material for MkDocs.
Documentation inspired by SQLModel.
However, we use other packages for development and other purposes. Check pyproject.toml to learn about all packages we use to build this package.
License
This project is licensed under the terms of the MIT license.
Metadata
Release files for mongodb-odm 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mongodb_odm-1.2.0.tar.gz | 50.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mongodb_odm-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 71.8 kB
Release files / mongodb_odm-1.2.0.tar.gz
| Download URL | mongodb_odm-1.2.0.tar.gz |
|---|---|
| Size | 50.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1542ad4a598b4e0620de00a4a9e393245fa6d8d37793c774b3d38b6c4df1a79d
|
|
BLAKE2b-256 checksum How to use checksums |
12971d54e631d87431eb764f072616ca25b200e4aec1999aca7d0d12b5f98014
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.5
|
Release files / mongodb_odm-1.2.0-py3-none-any.whl
| Download URL | mongodb_odm-1.2.0-py3-none-any.whl |
|---|---|
| Size | 21.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
49373ea480df0b897478f747877468edd59431cf268d7459971f60c8959fccd9
|
|
BLAKE2b-256 checksum How to use checksums |
3a3b0684f0fa8c9167ac9c32da1fbb9d611a2e941ae72d00ae9aa392b89c2278
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.5
|
Security for mongodb-odm 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.