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 https://img.shields.io/travis/jaraco/jaraco.modb/master.svg

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-3.6.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: jaraco.modb-3.6.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jaraco.modb-3.6.tar.gz
Algorithm Hash digest
SHA256 30a0a28fec973b85ba1567857d11534d292568cc942654758aea6188ca8e1970
MD5 ed5f3960679b4e088accc571978cad11
BLAKE2b-256 d0b98de74a27630519690c8df01745fedb1ba6b11d128937d0151a7828b7c22e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for jaraco.modb-3.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 71b6d01cadee5f28872759706ebf16f6c835868734a9cc18c358e0a6c12e8d7f
MD5 e2892d0aa17c88bb0d0bed853774897f
BLAKE2b-256 dfecfb47860b94774cfc3fd8770558703f2d49de7e159da07765f2769c41d628

See more details on using hashes here.

Supported by

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