python mongodb light工具包
MongoLite的Python项目详细描述
蒙古人是同一作者的一个分支。它的目标是通过剥离许多特性并用最佳实践替换它们来回归简单。
蒙古人的目标是尽可能地坚持使用pymongo api。 蒙古人总是选择速度超过合成糖,这就是为什么你看不到的原因。 此项目中的验证或点标记功能。
蒙古人是完美的谁想要一个薄薄的一层皮蒙哥,而不关心验证的东西。
蒙古人是美丽的stone
您的数据是干净的:
“工具改变,而不是数据”。为了遵循这个“信条”,就像 Mongokit,Mongolite不会将任何信息添加到保存到 数据库。所以如果你需要用其他的Mongo工具或者其他语言的ODM, 你的数据不会被蒙古人的东西污染。
功能
- schema less feature
- inheritance and polymorphisme support
- skeleton generation (your object is automaticaly filled by the correct fields)
- nested and complex schema declaration
- default values features
- random query support (which returns a random document from the database)
- json helpers
- GridFS support
一个简单的例子
文档声明如下:
>>> from mongolite import * >>> import datetime >>> connection = Connection() >>> @connection.register ... class BlogPost(Document): ... __database__ = 'test' ... __collection__ = 'example' ... skeleton = { ... 'title':unicode, ... 'body':unicode, ... 'author':unicode, ... 'date_creation':datetime.datetime, ... 'rank':int ... } ... optional = { ... 'tags': [unicode], ... } ... default_values = {'rank':0, 'date_creation':datetime.datetime.utcnow} ... >>> blogpost = con.BlogPost() # this use the db "test" from `__database__` and the collection "example" from `__collection__` >>> blogpost # the skeleton is automatically generated (based on the skeleton attribute) {'body': None, 'title': None, 'date_creation': datetime.datetime(...), 'rank': 0, 'author': None} >>> blogpost['title'] = u'my title' >>> blogpost['body'] = u'a body' >>> blogpost['author'] = u'me' >>> blogpost['tags'] = ['about me', 'first post'] >>> blogpost {'body': u'a body', 'title': u'my title', 'date_creation': datetime.datetime(...), 'rank': 0, 'author': u'me', 'tags': [u'about me', u'first post']} >>> blogpost.save()
注意,虽然文档中应该有skeleton中的字段,但默认情况下不会生成optional属性中的字段。它旨在仅用于文档…
要访问这些字段,请使用以下约定:
用于骨架中的字段:
>>> title = blogpost['title']
用于可选字段:
>>> tags = blogpost.get('tags', [])
蒙古人写在皮蒙哥的上面。所有pymongo的api都是可访问的,结果都被包装到文档对象中:
>>> blogpost = con.BlogPost.find_one() # this is a blogpost object
但是,如果需要更多性能,可以直接使用pymongo层:
>>> blogpost = con.test.example.find_one() # this is a dict
建议和补丁非常受欢迎。如果你在文件中发现错误 (英语不是我的主要语言)随时与我联系。你可以找到我(南望) 在twitter上。
最近的更改日志
v1.5
- support pymongo 2.4
v1.4
- support pymongo 2.3
- added fix for unconditional access to __wrap on cursors
- add __getitem__ suport on Cursor
v1.3
- change pymongo.objectid -> bson.objectid as this alias was deleted in pymongo 2.2
- add exception in case of bad behavior with inherited queries
v1.2
- fix generate_indexes() and slightly api change
v1.1
- add inherited queries support
- add serialize support
- fix pymongo’s version
v1.0
- fork from MongoKit, strip all unwanted features