MongoDB Object DataBase (MODB) for Python objects
Project description
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
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
Hashes for jaraco.modb-5.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe3955f4cc746fba516b1543ebfe336e39812e27e2e5ac157ebca3f1aa5cfdc5 |
|
MD5 | 8b96edba9c33e1349fe9fff5124a962c |
|
BLAKE2b-256 | f56d1a95e2988e278974e89b0c54cc46466037f06667b6fd7af1d040f5a5546d |