Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PyMongo and Flask-PyMongo libraries

I'm new to PyMongo and Flask, I have completed the tutorial for flask though and feel rather comfortable with it. I am now trying to implement a flask server with MongoDb and I'm not sure how to progress.

I see there are two libraries, PyMongo and Flask-PyMongo. It's isn't clear to me which I should use, or if I need to use both. How they interoperate and such...

First, i'm trying to connect to a mongodb, I have that running in the background and I can see connections whenever I start up my flask server, so that must be working. PyMongo offers methods to connect to specific database using db = client['test-database']. Flask-pymongo seems to just give a db connection from nowhere when using mongo = PyMongo(app) then mongo.db for access to the db.

Being new to mongo as well this is all confusing for me, I was hoping someone would be able to give me a clear answer to all my questions, searches around the web don't reveal many results for flask-pymongo library.

I did have a look at this question: What is the relationship between flask, mongokit, pymongo, flask-pymongo?, but it did not clear anything up for me.

like image 406
QweryBot Avatar asked Jul 31 '15 14:07

QweryBot


1 Answers

The main difference is that flask-pymongo is a wrapper of pymongo ready to work on flask's application environment.

You can configure the database connection within flask application config object.

Flask-pymongo also implements helper methods on top of pymongo..

For example: pymongo have find() method, while flask-pymongo have an extension named find_or_404() which raise a not found exception if the item does not exist, and so on..

like image 91
DobleL Avatar answered Oct 04 '22 19:10

DobleL