Repository URL to install this package:
|
Version:
0.9.1.2 ▾
|
mongokit
/
README.md
|
|---|

MongoDB is a great schema-less document oriented database. It has a lot of drivers for many languages (python, ruby, perl, java, php...).
MongoKit is a python module that brings a structured schema and validation layer on top of the great pymongo driver. It has been written to be as simple and light as possible with the KISS and DRY principles in mind.
MongoKit is designed to be:
Your data is clean:
"Tools change, not data". In order to follow this "credo", MongoKit won't add any information into your data saved into the database. So if you need to use other mongo tools or ODMs in other languages, your data won't be polluted by MongoKit's stuff.
Go to the full documentation
Documents are enhanced python dictionaries with a validate() method.
A Document declaration look as follows:
>>> from mongokit import * >>> import datetime >>> connection = Connection() >>> @connection.register ... class BlogPost(Document): ... structure = { ... 'title':unicode, ... 'body':unicode, ... 'author':unicode, ... 'date_creation':datetime.datetime, ... 'rank':int ... } ... required_fields = ['title','author', 'date_creation'] ... default_values = {'rank':0, 'date_creation':datetime.datetime.utcnow} ...
We establish a connection and register our objects.
>>> blogpost = con.test.example.BlogPost() # this uses the database "test" and the collection "example" >>> blogpost['title'] = u'my title' >>> blogpost['body'] = u'a body' >>> blogpost['author'] = u'me' >>> blogpost {'body': u'a body', 'title': u'my title', 'date_creation': datetime.datetime(...), 'rank': 0, 'author': u'me'} >>> blogpost.save()
Saving the object will call the validate() method.
And you can use a more complex structure as follows:
>>> @connection.register ... class ComplexDoc(Document): ... __database__ = 'test' ... __collection__ = 'example' ... structure = { ... "foo" : {"content":int}, ... "bar" : { ... 'bla':{'spam':int} ... } ... } ... required_fields = ['foo.content', 'bar.bla.spam']
Please see the tutorial for more examples.
Suggestions and patches are really welcome. If you find mistakes in the documentation (English is not my primary language) feel free to contact me. You can find me (namlook) on the freenode #mongodb irc channel or on twitter
__getstate__ and __setstate__ to DotedDict and i18nDotedDict. Problems appeared here when pickling mongokit documents due to apparent lack of these functions. (thanks to @petersng)__getitem__ on cursor with undefined __wrap (thanks to David T. Lehmann)__getitem__ on unwrapped cursor checks if __wrap is None (Merge pull request #97 from dtl/fix-getitem-on-unwrapped-cursor)Thank you all for all your patches !
use_schemaless feature ! please see the documentation for more information__database__ and __collection__ attributes for virtual documents