Flask-MongoAlchemy adds support for MongoDB on Flask using MongoAlchemy. Source code and issue tracking are available at Github. If you want to get started, check out the example source code.

Make sure you have MongoDB installed to use it.

If you prefer, you may use the latest source version by cloning the following git repository:

You can easily install using pip :

It is very easy and fun to use Flask-MongoAlchemy to proxy between Python and MongoDB.

All you have to do is create a MongoAlchemy object and use it to declare documents. Here is a complete example:

from flask import Flask from flask.ext.mongoalchemy import MongoAlchemy app = Flask ( __name__ ) app . config [ 'MONGOALCHEMY_DATABASE' ] = 'library' db = MongoAlchemy ( app ) class Author ( db . Document ): name = db . StringField () class Book ( db . Document ): title = db . StringField () author = db . DocumentField ( Author ) year = db . IntField ()

As you can see, extending the Document is all you need to create a document.

Now you can create authors and books:

>>> from application import Author , Book >>> mark_pilgrim = Author ( name = 'Mark Pilgrim' ) >>> dive = Book ( title = 'Dive Into Python' , author = mark_pilgrim , year = 2004 )

And save them:

>>> mark_pilgrim . save () >>> dive . save ()

If you make any changes on a document, you may call save() again:

>>> mark_pilgrim . name = 'Mark Stalone' >>> mark_pilgrim . save ()

And you can remove a document from the database by calling its remove() method:

>>> dive . remove ()

Another basic operation is querying for documents. Every document has a query class property. It’s very simple to use it:

>>> mark = Author . query . get ( '76726' ) >>> mark . name = 'Mark Pilgrim' >>> mark . save ()

You also can use the filter method instead of the get() method:

>>> mark = Author . query . filter ( Author . name == 'Mark Pilgrim' ) . first () >>> mark . name = 'Steve Jobs' >>> mark . save ()

Do you want to go further? Dive deep into the API docs.