Skip to main content

MongoDB Object DataBase (MODB) for Python objects

Project description

https://img.shields.io/pypi/v/jaraco.modb.svg https://img.shields.io/pypi/pyversions/jaraco.modb.svg tests Ruff Code style: Black https://img.shields.io/badge/skeleton-2023-informational

jaraco.modb is a small, pure-Python library for persisting Python objects to MongoDB.

Manual Usage

jaraco.modb facilitates using jsonpickle to produce MongoDB-friendly representations of pickleable Python objects for easy storage in a MongoDB database.

One may simply encode and decode Python objects to MongoDB BSON-friendly representations:

class MyObject(object):
    def __init__(self, val):
        self.val = val

import jaraco.modb
import pymongo
mongo_collection = pymongo.MongoClient().mydb.mycollection
val = MyObject(3)
# save the object to the DB
id = mongo_collection.save(jaraco.modb.encode(val))
# retrieve the object from the DB
new_val = jaraco.modb.decode(mongo_collection.find_one(id))
assert isinstance(new_val, MyObject)
assert new_val.val == 3

A more detailed tutorial is now published as a Jupyter Notebook.

Automatic Usage

jaraco.modb also provides an SON Manipulator suitable for automatically encoding arbitrary objects for a pymongo.Database:

jaraco.modb.SONManipulator.install(mongo_collection.database)
mongo_collection.save({'val': val})

Unfortunately, due to a limitation with the API of the SONManipulator, it’s not possible to save a custom object as the document itself (the document must always be a dict).

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

jaraco.modb-6.1.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

jaraco.modb-6.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file jaraco.modb-6.1.0.tar.gz.

File metadata

  • Download URL: jaraco.modb-6.1.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for jaraco.modb-6.1.0.tar.gz
Algorithm Hash digest
SHA256 55f0d730c05150012b33b814746561bf574bc2399fb5e69a50b13d8c6cb8f8b5
MD5 3b88ef86df27be9f25583428dcb20946
BLAKE2b-256 bec4d93330b6f0d93b78d6644dc926fe7c07c3d3314b891df5ed2e53a25ccb6f

See more details on using hashes here.

File details

Details for the file jaraco.modb-6.1.0-py3-none-any.whl.

File metadata

  • Download URL: jaraco.modb-6.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for jaraco.modb-6.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fc59eb0e81ef1a6964102813adb9b23129c6401b8dfcddd5c20497ead23c4b12
MD5 132adfc277061d0ccd16c1deb186889d
BLAKE2b-256 98413e8ffe61520c112fc740b57a3f24c36429d3bac83cfac782e8cd89cc2ee5

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