mongodbflaskmongoenginemongokit

Flask and Mongo


Thinking of a web service entirely built on top of MongoDB, while I am pretty confortable with PyMongo, I would like to know if you guys have any positive or negative experiences/stories about either of these ODMs: MongoKit, MongoEngine and MongoAlchemy, the latter having a Flask specific package "Flask-mongoalchemy".


Solution

  • I don't really have any real experience or story to offer, but i played with both MongoKit and MongoAlchemy, and i personally decided to try MongoAlchemy, because i like the syntax a little better (probably due to my Django heritage).


    MongoKit:

    class BlogPost(Document):
        structure = {
                    'title':unicode,
                    'body':unicode,
                    'author':unicode,
                    'date_creation':datetime.datetime,
                    'rank':int
                    }
    


    MongoAlchemy:

    class BloodDonor(Document):
        first_name = StringField()
        last_name = StringField()
        age = IntField(min_value=0)
        gender = EnumField(StringField(), 'male', 'female')
        blood_type = EnumField(StringField(), 'O+','A+','B+','AB+',)
    


    Both will help you to validate your data, will let you impose something like a schema (only on application level), and will save you some typing (specifically brackets).

    MongoKit is more complete. I chose MongoAlchemy because I didn't want to type structure = {} all the time, and specifying your db and collection using con.test.example.BlogPost() just felt wrong (although you don't have to do it this way).

    Try both, and choose the one which works better for you.

    As you already mentioned, there is a Flask-MongoAlchemy extension, which works great. If you want to use MongoKit, the excellent Flask documentation will get you going in no time: http://flask.pocoo.org/docs/patterns/mongokit/

    The great thing is that you just can try one, if you don't like it you can switch to another, or drop to pymongo without having to change anything in the database.