MongoDB-ODM, NOSQL databases in Python, designed for simplicity, compatibility, and robustness.
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongodb_odm-1.2.0.tar.gz.
File metadata
- Download URL: mongodb_odm-1.2.0.tar.gz
- Upload date:
- Size: 50.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1542ad4a598b4e0620de00a4a9e393245fa6d8d37793c774b3d38b6c4df1a79d
|
|
| MD5 |
b9ec2d3c65b983cd9552e8cc7eba4a81
|
|
| BLAKE2b-256 |
12971d54e631d87431eb764f072616ca25b200e4aec1999aca7d0d12b5f98014
|
File details
Details for the file mongodb_odm-1.2.0-py3-none-any.whl.
File metadata
- Download URL: mongodb_odm-1.2.0-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49373ea480df0b897478f747877468edd59431cf268d7459971f60c8959fccd9
|
|
| MD5 |
6ac532824c4d495d4c9d4c66c367847f
|
|
| BLAKE2b-256 |
3a3b0684f0fa8c9167ac9c32da1fbb9d611a2e941ae72d00ae9aa392b89c2278
|