DEV Community

Cover image for Connecting Django with MongoDB: All the Versioning Shenanigans Dissected (2022 edition)

Posted on


Connecting Django with MongoDB: All the Versioning Shenanigans Dissected (2022 edition)

Trust me, I have tried connecting Django with MongoDB using pymongo, mongoengine, and djongo finally. Although MongoDB’s website gives in-depth and stepwise instructions to do it, there are some versioning issues that need to be sorted out before aiming for that “Huge” (Mongo literally translates to huge 😛) Database to do wonders for your project.

Here I am assuming you are using the latest Django version 2.2.26 and Python version 3.6+

Skip Step 1 if you already have MongoDB installed. Please check the version once to have a better understanding of the 2 drivers that we will use to connect with Django.

To check MongoDB version use-

mongod --version

Step 1

Install MongoDB on local from terminal using the following command-

brew install mongodb-community@5.0

For Windows, you need to get the installer and follow the steps as given here

Step 2

Download MongoDB Compass from here. It gives a user-friendly interface like SQLWorkbench and you can even run a MongoDB cluster for free from there. The application looks something like this-

MongoDB Compass Interface

Step 3

Connecting to a Django Project

  • Using Djongo

Install djongo through terminal-

pip install djongo
Enter fullscreen mode Exit fullscreen mode

Coming to actually connecting MongoDB with your Django project using djongo, all we need to do is add the following to the file-

          'default': {
            'ENGINE': 'djongo',
            'NAME': 'YOUR_DATABASE_NAME',
            'HOST': 'localhost',
            'PORT': '27017'
Enter fullscreen mode Exit fullscreen mode
  • Using MongoEngine

    To connect MongoDB with your Django project using mongoengine, you need to add the following to the file-

    _MONGODB_USER = ""
    _MONGODB_HOST = "localhost"
    _MONGODB_PORT = 27017
    _MONGODB_DATABASE_HOST = "mongodb://%s:%s@%s/%s" % (

    mongoengine.connect(_MONGODB_NAME, host=_MONGODB_HOST, port=_MONGODB_PORT)
Enter fullscreen mode Exit fullscreen mode

After that you can import it in your file and use it-

Mongoengine usage in file

  • Using PyMongo

    The latest pymongo version is 4.0.2 which is incompatible with the latest mongoDB v5.0. I got this error after which I downgraded the pymongo version.

    pymongo.errors.ConfigurationError: Server at xxxx:xx reports wire version 5, but this version of PyMongo requires at least 6 (MongoDB 3.6).

    pymongo v 3.12.1

    MongoDb v 5.0.5

    Source -

    Because of versioning shenanigans I was turned off.

Key Takeaways

  1. I would highly recommend using mongoengine for its ease to access collections through models.
  2. Djongo is very easy to connect with Django. ( I had tried to connect all three and somehow pymongo’s version incompatibility wouldn’t allow me to connect with djongo)

    Apparently, Djongo has dependencies on pymongo and sqlparse so we need to get the version right for pymongo after installing Djongo-

Djongo + sqlparse + Pymongo

Now if you go up and check pymongo’s versioning issues, you will know that MongoDB v5.0 is not compatible with PyMongo’s v 4.0

To downgrade pymongo’s version run this command-

python3 -m pip install pymongo==3.12.1
Enter fullscreen mode Exit fullscreen mode
  1. Hence using the following combinations did wonders for me -
    • Djongo v 1.3.6
    • Pymongo v 3.12.1
    • MongoDB v 5.0.5

Top comments (1)

ailyamas profile image

How to do this using Django 3.0 or higher

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.