Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nayan32biswas/mongodb-odm
MongoDB-ODM, NOSQL databases in Python, designed for simplicity, compatibility, and robustness.
https://github.com/nayan32biswas/mongodb-odm
mongodb python
Last synced: about 1 month ago
JSON representation
MongoDB-ODM, NOSQL databases in Python, designed for simplicity, compatibility, and robustness.
- Host: GitHub
- URL: https://github.com/nayan32biswas/mongodb-odm
- Owner: nayan32biswas
- License: mit
- Created: 2022-11-04T06:56:02.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-15T12:12:24.000Z (4 months ago)
- Last Synced: 2024-09-30T12:41:22.155Z (about 2 months ago)
- Topics: mongodb, python
- Language: Python
- Homepage: https://mongodb-odm.readthedocs.io
- Size: 393 KB
- Stars: 17
- Watchers: 3
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.---
## Requirement
**MongoDB-ODM** will work on Python 3.8 and above
This **MongoDB-ODM** is built on top of **PyMongo** and **Pydantic**. Those packages are required and will auto-install while **MongoDB-ODM** was installed.
## Installation
```console
$ pip install mongodb-odm
```## Example
### Define model
```Python
import os
from typing import Optionalfrom mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = Noneclass ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("rating", ASCENDING)]),
]
```### Set Connection
```Python
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
```### Create Document
```Python
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
```Python
for player in Player.find():
print(player)
```#### Find one object with filter
```Python
player = Player.find_one({"name": "Pelé"})
```### Update Data
```Python
player = Player.find_one({"name": "Pelé"})
if player:
player.rating = 98 # potential
player.update()
```### Delete Data
```Python
player = Player.find_one({"name": "Pelé"})
if player:
player.delete()
```### Apply Indexes
```Python
import os
from typing import Optionalfrom mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = Noneclass ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("rating", ASCENDING)]),
]
```- To create indexes in the database declare [IndexModel](https://pymongo.readthedocs.io/en/stable/tutorial.html#indexing) and assign in indexes array in ODMConfig class. **IndexModel** modules that are directly imported from **pymongo**.
- Import the `apply_indexes` from `mongodb_odm`. Call the `apply_indexes` function from your CLI. You can use Typer to implement CLI.## Example Code
This is the example of full code of above.
```python
import os
from typing import Optionalfrom mongodb_odm import ASCENDING, Document, IndexModel, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = Noneclass 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({"name": "Pelé"})
if player:
player.rating = 98 # potential
player.update()player = Player.find_one({"name": "Pelé"})
if player:
player.delete() # RIP
```### Supported Framework
**MongoDB-ODM** is not framework dependent. We can use this package in any system. But we take special consideration being 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.
But we use other packages for development and other purposes. Check **pyproject.toml** to know about all packages we use to build this package.
## License
This project is licensed under the terms of the [MIT license](https://github.com/nayan32biswas/mongodb-odm/blob/main/LICENSE).