HumbleDB solves the age-old MongoDB dilemma: write readable, maintainable code or optimize for storage efficiency. With HumbleDB, you get both. This lightweight ODM lets you use clear, descriptive attribute names in your Python code while automatically mapping them to ultra-short database keys, dramatically reducing document size and memory usage without sacrificing code clarity.
Warning
Version 7.0.0 Breaking Changes. This release updates to Pymongo 4.x, which introduces breaking changes. While we've maintained all older 3.x and 2.x Pymongo methods with identical signatures where possible, there may be other breaking changes not covered by our test suite. If you encounter any issues during upgrade, please open an issue.
Efficient Storage: Map readable attribute names to ultra-short database keys, reducing document size and memory usage while maintaining code clarity.
Full Pymongo Compatibility: Maintains backwards-compatible methods for
Pymongo 4.x including insert
, find_and_modify
, save
, and other familiar
operations.
Maximum Flexibility: Documents are also dictionaries - no schema restrictions, maximum adaptability to changing requirements.
Thread & Greenlet Safe: Built for concurrent applications with safe connection handling and resource management.
Context-Managed Connections: Connection paradigm minimizes socket usage from the connection pool through explicit context managers.
Lightweight Design: T 9CF2 hin wrapper around Pymongo that exposes the full power of the underlying driver without performance overhead.
Create a Document
subclass with readable attribute names mapped to short database keys:
from humbledb import Mongo, Document
class TestDoc(Document):
config_database = 'test' # Target database
config_collection = 'testdoc' # Target collection
test_key = 't' # Maps 'test_key' attribute to 't' in MongoDB
other_key = 'o' # Maps 'other_key' attribute to 'o' in MongoDB
Documents work like regular Python objects with attribute access, while storing data efficiently:
doc = TestDoc()
doc.test_key = 'Hello'
doc.other_key = 'World'
# View the actual MongoDB document structure
print(doc)
# TestDoc({'t': 'Hello', 'o': 'World'})
Access your data through mapped attributes or direct dictionary keys:
# Via mapped attributes (recommended)
print(doc.test_key) # 'Hello'
# Via dictionary access
print(doc['t']) # 'Hello'
print(doc['o']) # 'World'
Use the Mongo
context manager for safe database operations:
# Insert document
with Mongo:
TestDoc.insert(doc)
# Query documents
with Mongo:
found = TestDoc.find_one()
print(found)
# TestDoc({'_id': ObjectId('50ad81586112797f89b99606'), 't': 'Hello', 'o': 'World'})
See the documentation for more examples and detailed explanations.
The complete documentation can be found on http://humbledb.readthedocs.org.
See LICENSE.rst.