Skip to main content

Document object mapper for pydantic and pymongo

Project description

Pydantic Mongo

Build Status Maintainability Test Coverage Version Downloads

Document object mapper for pydantic and pymongo

Usage

Install:

pip install pydantic-mongo

Example Code

from bson import ObjectId
from pydantic import BaseModel
from pydantic_mongo import AbstractRepository, PydanticObjectId
from pymongo import MongoClient
from typing import Optional, List
import os

class Foo(BaseModel):
   count: int
   size: float = None

class Bar(BaseModel):
   apple: str = 'x'
   banana: str = 'y'

class Spam(BaseModel):
   # PydanticObjectId is an alias to Annotated[ObjectId, ObjectIdAnnotation]
   id: Optional[PydanticObjectId] = None
   foo: Foo
   bars: List[Bar]

class SpamRepository(AbstractRepository[Spam]):
   class Meta:
      collection_name = 'spams'

client = MongoClient("mongodb://localhost:27017")
database = client["example"]

spam = Spam(foo=Foo(count=1, size=1.0),bars=[Bar()])

spam_with_predefined_id = Spam(
   id=ObjectId("611827f2878b88b49ebb69fc"),
   foo=Foo(count=2, size=2.0),
   bars=[Bar()]
)

spam_repository = SpamRepository(database=database)

# Insert / Update
spam_repository.save(spam)

# Insert / Update many items
spam_repository.save_many([spam, spam_with_predefined_id])

# Delete
spam_repository.delete(spam)

# Find One By Id
result = spam_repository.find_one_by_id(spam.id)

# Find One By Id using string if the id attribute is a PydanticObjectId
result = spam_repository.find_one_by_id(ObjectId('611827f2878b88b49ebb69fc'))
assert result.foo.count == 2

# Find One By Query
result = spam_repository.find_one_by({'foo.count': 1})

# Find By Query
results = spam_repository.find_by({'foo.count': {'$gte': 1}})

# Paginate using cursor based pagination
edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1)
more_edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1, after=list(edges)[-1].cursor)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_mongo-2.3.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

pydantic_mongo-2.3.0-py2.py3-none-any.whl (7.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pydantic_mongo-2.3.0.tar.gz.

File metadata

  • Download URL: pydantic_mongo-2.3.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pydantic_mongo-2.3.0.tar.gz
Algorithm Hash digest
SHA256 d1b05d6bf28e0eeca66809a99c362b958bf6fc6af59350d44d9a948e5fc30e3b
MD5 6c77eec5c57cceaa029c49a5f361b1dc
BLAKE2b-256 a507aaa68bed11c819ca1330b304a59038368d5b8617bd7f879a60fb1d4f0fd5

See more details on using hashes here.

File details

Details for the file pydantic_mongo-2.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_mongo-2.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2974740e7c922db34c39bc9235b13aa13f12526c7b2dc24bd9ca69eebb7c9a20
MD5 c1a6519a26d3428d3b504d6fdf0ae609
BLAKE2b-256 6889713b13de2dcf9c5ed5d4ece54516d9e1c94dbf10a6c32c601640b6cfb948

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page