MongoKit vs MongoEngine vs Flask-MongoAlchemy for Flask

Does anyone have experience with MongoKit, MongoEngine or Flask-MongoAlchemy for Flask?

Which one do you prefer? Positive or negative experiences ?. Too many options for Flask-Newbie.

+61
python flask mongodb sqlalchemy
Feb 25 2018-12-12T00:
source share
1 answer

I spent a lot of time evaluating the popular Python ORM for MongoDB. This was an exhaustive exercise, as I really wanted to choose it.

My conclusion is that ORM removes interest from MongoDB. Nobody feels natural, they impose restrictions similar to those that made me leave relational databases in the first place.

Again, I really wanted to use ORM, but now I am convinced that using pymongo directly is the way to go. Now I follow the pattern that covers MongoDB, pymongo and Python.

Resource-oriented architecture leads to a very natural view. For example, take the following user resource:

 from werkzeug.wrappers import Response from werkzeug.exceptions import NotFound Users = pymongo.Connection("localhost", 27017)["mydb"]["users"] class User(Resource): def GET(self, request, username): spec = { "_id": username, "_meta.active": True } # this is a simple call to pymongo - really, do # we need anything else? doc = Users.find_one(spec) if not doc: return NotFound(username) payload, mimetype = representation(doc, request.accept) return Response(payload, mimetype=mimetype, status=200) def PUT(self, request, username): spec = { "_id": username, "_meta.active": True } operation = { "$set": request.json, } # this call to pymongo will return the updated document (implies safe=True) doc = Users.update(spec, operation, new=True) if not doc: return NotFound(username) payload, mimetype = representation(doc, request.accept) return Response(payload, mimetype=mimetype, status=200) 

Resource base class looks like

 class Resource(object): def GET(self, request, **kwargs): return NotImplemented() def HEAD(self, request, **kwargs): return NotImplemented() def POST(self, request, **kwargs): return NotImplemented() def DELETE(self, request, **kwargs): return NotImplemented() def PUT(self, request, **kwargs): return NotImplemented() def __call__(self, request, **kwargs): handler = getattr(self, request.method) return handler(request, **kwargs) 

Note that I use the WSGI specification directly and use Werkzeug where possible (by the way, I think Flask adds unnecessary Werkzeug complication).

The representation function accepts Accept request headers and creates a suitable view (e.g. application/json or text/html ). It is not difficult to implement. It also adds the Last-Modified header.

Of course, your contribution needs to be disinfected, and the code presented will not work (I mean this as an example, but it is not difficult to understand my point of view).

Again, I tried everything, but this architecture made my code flexible, simple, and extensible.

+76
Feb 25 '12 at 20:27
source share
— -



All Articles