12

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.

Community
  • 1
  • 1
QweryBot
  • 457
  • 1
  • 4
  • 15
  • 1
    I haven't used Mongo with Flask but you are going to want to use the Flask extension. There is usually some small differences in `flask-*` modules compared to their "standard" module. They are generally speaking, a wrapper around the core module that makes them interact Flask a little better. For example `flask-sqlalchemy` will still install normal `SQLAlchemy` and the API is slightly different but underneath it's all working approximately the same. – kylieCatt Jul 31 '15 at 14:17
  • Okay, this does help explain it a little. I think the biggest problem is not having knowledge of MongoDB, then having `PyMongo` and `Flask-PyMongo` thrown at me. Thanks to your help I now have a direction to go in at least, I will aim to read some PyMongo documentation then move onto Flask-PyMongo, I think the wrapper will be easier to understand if I know the background first. – QweryBot Jul 31 '15 at 14:32
  • 1
    As it reads in the [docs](https://flask-pymongo.readthedocs.org/en/latest/#quickstart): `Flask-PyMongo depends, and will install for you, recent versions of Flask (0.8 or later) and PyMongo (2.4 or later)`. – doru Jul 31 '15 at 17:39
  • 3
    I use Flask-Pymongo and my colleague just PyMongo so we thought about what the differences might be. I assume, though I haven't tested it, that Flask-Pymongo will take care of multiple accesses to the mongodb connection from the same flask web server as multiple urls hit the server. Using Pymongo this might need to be handled programmatically unless new connections are made for every request. I suppose that Flask-Pymongo just integrates with the multi-user aspects of flask automatically. – jimscafe Aug 01 '15 at 02:51
  • Both of them should be included in your application. Flask-pymingo could not work if PyMpngo does not exist. – Light.G Oct 17 '18 at 05:35

1 Answers1

9

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

ngShravil.py
  • 4,742
  • 3
  • 18
  • 30
DobleL
  • 3,808
  • 14
  • 20