How to Connect MongoDB Database to Django?

Published On: 07/10/2022 | Category: Django

Hi Dev,

I am going to show you example of how to connect mongodb database to django. I would like to show you how to connection mongodb database with django. if you have question about how to connect a django app to mongodb with djongo then I will give simple example with solution. you will learn how to connect a django app to mongodb with mongoengine.

Let's imagine that you are engaged in a real-time project. You must work with unstructured data that contains millions of records. MongoDB enters the scene at this point. It can easily store and read unstructured data. Now that we have a foundational understanding, let's look more closely at MongoDB and Django.

  • MongoEngine
  • PyMongo
  • Djongo

Let's see bellow example I explained simply about how to connect mongodb database to django.


Working with MongoDB from Python requires the use of MongoEngine, a Document-Object Mapper (think ORM but for document databases).install the mongoengine through following command..

pip install -u mongoengine

Open the file after beginning the Django project and add these lines. As indicated in the code sample below, remove or comment out the DATABASES section as well.
import mongoengine

mongoengine.connect(db=DATABASE_NAME, host=DATABASE_HOST, username=USERNAME, password=PASSWORD)

# 'default': {
# 'ENGINE': 'djongo', #'django.db.backends.sqlite3',
# 'NAME': 'blogs', # DB name
# 'USER': 'root', # DB User name 
# }

Since you are utilising MongoEngine ORM, there is no need to migrate and make migrations.

Also, your file will look as shown in below code snippet.
from mongoengine import Document, fields

class Blogs(Document):
    name = fields.StringField()
    topic = fields.StringField()
    date = fields.DateTimeField()
    addition_info = fields.DictField()


This tool includes the Python distribution needed to work with MongoDB. This works well to save and write JSON data to your MongoDB. You may use all of the mongo queries in your code thanks to this tool.

pip install pymongo

Do the following after beginning the Django project add these lines to after creating it in the project folder.

from pymongo import MongoClient
def get_db_handle(db_name, host, port, username, password):
    client = MongoClient(host=host,
    db_handle = client[db_name]
    return db_handle, client

def get_collection_handle(db_handle,collection_name):
    return db_handle[collection_name]

The aforementioned features can now be used in your views. Anywhere you wish to fetch or write data into your MongoDB is acceptable.

from project.utils import get_db_handle, get_collection_handle

db_handle, mongo_client = get_db_handle(DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, USERNAME, PASSWORD)
collection_handle = get_collection_handle(db_handle, REGIONS_COLLECTION)

When utilising PyMongo, project/ doesn't require any action. Additionally, there is no need to modify anything to project/; however, don't forget to remove the DATABASES element from the file by adding a comment.


Here, we'll go over how to set up Django, install MongoDB, and establish a connection.

Install Djongo:

pip install djongo

Open the file by going to your project folder (for example, MyFirstDjangoProj). It is editable using Textpad, the Python IDE, or any other editor. Change the settings to point to MongoDB by searching for DATABASES and doing so. Your MongoDB database name will be the database name (NAME) and the engine will be djongo.

       'default': {
           'ENGINE': 'djongo',
           'NAME': 'db-name',

Now that we have the Django project (and app), you can create the collections in MongoDB using the commands:

python makemigrations
python migrate

Set ENFORCE SCHEMA: False in your database settings to make Djongo migration-free. With this configuration, Djongo won't translate SQL statements into MongoDB commands and the collections are built instantly.

I Hope It will help you....