namlook / mongokit

MongoKit framework try to keep its simplicity when you manage mongodb in python. MongoKit was developed to be fast and light with KISS and DRY in mind. MongoKit brings structured schema and validation layer on top of the great pymongo driver. Discuss with us on Google group : http://groups.google.com/group/mongokit or follow the news on Twitter: http://twitter.com/namlook
http://namlook.github.com/mongokit/
Other
677 stars 132 forks source link

maintainers wanted ! See #182 for more infos.

MongoKit Build Status

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.

Philosophy

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.

Features

Go to the full documentation

A quick example

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

Recent Change Log

v0.9.1

v0.9.0

v0.8.3

v0.8.2

v0.8.1

v0.8.0

Thank you all for all your patches !

v0.7.2

v0.7.1

v0.7