Peter Bengtsson: Using MongoDB in your appΒΆ

Tags: django, djangocon, zope

Peter Bengtsson picked MongoDB out of the available NoSQL databases just as it seemed to be the best for his needs at the time. He had already 10 years of NoSQL experience: zope’s ZODB object database (which stores python pickles).

A NoSQL database matters as it can be much more performant than an SQL database. MongoDB has some features that make it easier to step over from an SQL database. Internally it uses BSON (JSON + some binary types, for instance datetime). You can set it up in a master/slave configuration.

A big thing about MongoDB is that it is document-oriented, so it isn’t a simple key/value store and it also isn’t a sql-like database with fixed columns.

MongoDB has some implications:

  • No joins. Your code suddenly needs to work with pagination.
  • No transactions. You might need to change the way you code. You cannot rely on rolling back a transaction, so perhaps your views have to do smaller things. The speed advantage is huge, though, so it is also easier to write smaller changes more often.
  • Polymorphism. It makes inheritance workable. Inheritance of structure (awesome, according to Peter). You can ask for all “Pages”, but also for all “WikipediaPages” if that’s a subclass.
  • IDs are hashes, not integers. So you’ll have to change your \d+ regexes in your urls.py.
  • The Django admin is one of the things that initially stop working when you use MongoDB. Flatpages, piston, celery, etc. You can change things, though, to get them working again.

The python bindings are explained in the API docs of pymongo. You probably also need one of the “ORM”s for python and MongoDB. See the ming ORM or an article by Peter about mongoengine and django-mongokit.

Peter has a suggestion on what to use when starting with NoSQL: MongoEngine. It is the least verbose and it has automatic collection management. Django-mongoforms is nice for forms.

Look at the Django non-rel project that released something nice just yesterday. So Peter warns that some of his talk might be overtaken by reality by that. So look it up!

Watch tower in 1990

(About the photo: 1990 school trip to Berlin)

blog comments powered by Disqus
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):