Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask and Mongo [closed]

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".

like image 641
arturhoo Avatar asked Jul 13 '11 02:07

arturhoo


People also ask

Can you use MongoDB with Flask?

You'll use it with Flask to perform basic tasks, such as connecting to a database server, creating collections that store a group of documents in MongoDB, inserting data to a collection, and retrieving and deleting data from a collection.

What is Flask_pymongo?

MongoDB is an open source database that stores flexible JSON-like “documents,” which can have any number, name, or hierarchy of fields within, instead of rows of data as in a relational database.

Which is better PyMongo or MongoEngine?

Both PyMongo and MongoEngine can be used to access data from a MongoDB database. However, they work in very different ways and offer different features. PyMongo is the MongoDB recommended library. It makes it easy to use MongoDB documents and maps directly to the familiar MongoDB Query Language.


2 Answers

I use MongoEngine with flask no problems. We've written (collected resources) which include wtform support and flask-debugger support as well:

https://github.com/MongoEngine/flask-mongoengine/

like image 174
Ross Avatar answered Oct 03 '22 16:10

Ross


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.

like image 23
jeverling Avatar answered Oct 03 '22 16:10

jeverling