Mongotraits
A liteweight Object Document Manager for MongoDB and Python.
Install / Use
/learn @Zaharid/MongotraitsREADME
Mongotraits
A liteweight simple Object Document Manager to connect Python objects to MongoDB, similar to mongoengine
Features
- Based on IPython Traitlets.
- User interface in IPython Notebook for creating and adding objects via the widgetrepr package.
- Objects with the same id are always the same python object.
- References are special TraitType subclasses that implement a dereference and a ref methods.
- References to embedded objects are possible.
- Arbitrary objects can be saved: They either implement a savedict and to_classdict methods, are represented unchanged if they are of the right type (SAME_TYPES) or they are pickled.
- Container traits are handled correctly.
The drawbacks I can think of are:
- The performance has not been taken into account at all.
- No fancy query meta language (but pymongo is easy enough to use).
Dependencies
Install
Download the package and run python setup.py install.
Example
import datetime
import mongotraits
from IPython.utils import traitlets
mongotraits.connect('test')
class BlogPost(mongotraits.Document):
author = traitlets.Unicode()
title = traitlets.Unicode()
content = traitlets.Unicode()
references = traitlets.List(mongotraits.Reference(__name__ + '.BlogPost'))
my_post = BlogPost(author = 'Zah', title = "First Post", content = "Mongotraits is nice and easy")
other_post = BlogPost(author = 'Zah', title = "Second Post", content = "Another post", references = [my_post,])
my_post.save()
other_post.save()
#The object is already saved in MongoDB
del other_post
#Can find using
loaded_post= BlogPost.find_one({'title':"Second Post"})
#Same id always corresponds to the same Python object.
assert(loaded_post.references[0] is my_post)
Known bugs
Circular references do not work properly at the moment.
Related Skills
node-connect
349.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
