Which Python API should be used with Mongo DB and Django

2024/11/20 1:50:59

I have been going back and forth over which Python API to use when interacting with Mongo. I did a quick survey of the landscape and identified three leading candidates.

  • PyMongo
  • MongoEngine
  • Ming

If you were designing a new content-heavy website using the django framework, what API would you choose and why?

MongoEngine looks like it was built specifically with Django in mind. PyMongo appears to be a thin wrapper around Mongo. It has a lot of power, though loses a lot of the abstractions gained through using django as a framework. Ming represents an interesting middle ground between PyMongo and MongoEngine, though I haven't had the opportunity to take it for a test drive.

Answer

As Mike says, you can't avoid PyMongo - all the other interfaces build on top of it. These other interfaces are arguably unnecessary. ORMs such as that used in Django are useful when dealing with SQL because they mitigate the complexity of creating SQL queries and schemas, and parsing result sets into objects.

PyMongo however already has that covered - queries go through a convenient and simple API and results coming from MongoDB already are objects (well, dicts in Python - same difference) by definition. If you feel that you really need to decorate your Mongo documents with Python objects, it's easy to add a SON manipulator to PyMongo. The nice thing about this approach is that you can write code directly on PyMongo, and slide in additional functionality later on without having to insert a new API between your code and PyMongo.

What's left? Schema creation and migration are somewhat useful, but are almost as simply done ad-hoc - chances are if you're considering using MongoDB you want to break out of the traditional SQL-style model anyway. Also, if there were a fully Django-compatible MongoDB ORM you might get some mileage out of it. Anything less than that and you will probably be creating work for yourself.

You won't regret using PyMongo directly.

One last option worth watching if you are interested in top efficiency is the asynchronous version of PyMongo, here: http://github.com/fiorix/mongo-async-python-driver

https://en.xdnf.cn/q/26372.html

Related Q&A

Save a list to a .txt file

Is there a function in python that allows us to save a list in a txt file and keep its format?If I have the list:values = [1,2,3]can I save it to a file that contains:[1,2,3]So far I print parts of th…

How to run a python script like pm2 for nodejs

Ive used pm2 for my Node.js script and I love it. Now I have a python script which collect streaming data on EC2. Sometimes the script bombs out and I would like a process manager to restart itself lik…

Resize rectangular image to square, keeping ratio and fill background with black

Im trying to resize a batch of grayscale images that are 256 x N pixels (N varies, but is always ≤256). My intention is to downscale the images.The resize would have to output a square (1:1) image, wi…

How to capture arbitrary paths at one route in FastAPI?

Im serving React app from FastAPI by mounting app.mount("/static", StaticFiles(directory="static"), name="static")@app.route(/session) async def renderReactApp(request: Re…

Changing the active class of a link with the twitter bootstrap css in python/flask

I got the following html snippet from my page template.html.<ul class=nav><li class="active"><a href=/>Home</a></li><li><a href=/lorem>Lorem</a>…

overwriting a spark output using pyspark

I am trying to overwrite a Spark dataframe using the following option in PySpark but I am not successfulspark_df.write.format(com.databricks.spark.csv).option("header", "true",mode=…

How to patch a constant in python

I have two different modules in my project. One is a config file which containsLOGGING_ACTIVATED = FalseThis constant is used in the second module (lets call it main) like the following:if LOGGING_ACTI…

Python script for minifying CSS? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

Saving numpy array to txt file row wise

I have an numpy array of forma = [1,2,3]which I want to save to a .txt file such that the file looks like:1 2 3If I use numpy.savetxt then I get a file like:1 2 3There should be a easy solution to this…

I want Python argparse to throw an exception rather than usage

I dont think this is possible, but I want to handle exceptions from argparse myself.For example:import argparse parser = argparse.ArgumentParser() parser.add_argument(--foo, help=foo help, required=Tru…