Connecting Django with MongoDB: All the Versioning Shenanigans Dissected (2022 edition)
Tags: mongodb, django, python, programming
Published: 2022-03-24 11:21:32

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](https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/)

### Step 2

Download MongoDB Compass from [here](https://www.mongodb.com/try/download/compass). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i20q6jo49cgpq21pcwn.jpeg)

### Step 3

Connecting to a Django Project

- **Using Djongo**
    
Install djongo through terminal-
   
    pip install djongo
    
Coming to actually connecting MongoDB with your Django project using djongo, all we need to do is add the following to the settings.py file-
    
 

```
    DATABASES = {
          'default': {
            'ENGINE': 'djongo',
            'NAME': 'YOUR_DATABASE_NAME',
            'HOST': 'localhost',
            'PORT': '27017'
        }
    }
```
    

- **Using MongoEngine**
    
    To connect MongoDB with your Django project using mongoengine, you need to add the following to the settings.py file-
    
    

```
    _MONGODB_USER = ""
    _MONGODB_PASSWD = ""
    _MONGODB_HOST = "localhost"
    _MONGODB_NAME = "YOUR_DATABASE_NAME"
    _MONGODB_PORT = 27017
    _MONGODB_DATABASE_HOST = "mongodb://%s:%s@%s/%s" % (
        _MONGODB_USER,
        _MONGODB_PASSWD,
        _MONGODB_HOST,
        _MONGODB_NAME,
    )
    
    mongoengine.connect(_MONGODB_NAME, host=_MONGODB_HOST, port=_MONGODB_PORT)
```
After that you can import it in your models.py file and use it-
    
   
![Mongoengine usage in models.py file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ep3j477c9cg38fxcuctd.png)
    
- **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 - [pypi.org](https://pypi.org/project/pymongo/)
    
    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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1pekbvdg08qwo3qtbfs.png)
    
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
    
    
3. Hence using the following combinations did wonders for me -
    - Djongo v 1.3.6
    - Pymongo v 3.12.1
    - MongoDB v 5.0.5