Using MongoDB in Django Application

mongodb in django

In previous article we learned how to install Elasticsearch and Kibana and connect them together. In second part of the same article we used Elasticsearch in Django project.

Elasticsearch can be used with relational database or non-relational database. In those articles we used relational database MySQL.

In this article we will see how to use MongoDB in Django application.

I use PythonAnyWhere to host Django application quickly. Read here how to host your Django app on PythonAnyWhere for free.

What is MongoDB:

As per Wikipedia –

MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas.

MongoDB stores data in JSON-like documents. Structure of these documents is flexible, meaning fields can vary from document to document.

mongodb features
Quick Introduction:

Go through these slides to have a quick introduction of MongoDB.

Installing MongoDB:

Follow these instructions to install latest stable version of MonboDB on your Ubuntu machine.

  • We need to add MongoDB repository to our server and in order to do that we need to import the key for official MongoDB repository.

Import the key by running below command in your terminal.

  • Add MongoDB repository to package list so that apt know the location to download from.

  • Update the package list and install MongoDB.

  • MongoDB log files are created in /var/log/mongodb  and data files in /var/lib/mongodb  directory. These settings are defined in /etc/mongod.conf file.
Starting MongoDB:

After installation is complete, start the MongoDB.

Keep an eye on /var/log and if it says ‘waiting for connection on port 27017’ that means MongoDB is ready.

You can check the status of MongoDB using below command as well.

You might face some issues while starting MongoDB. Make sure you create a systemctl  file   /etc/systemd/system/mongodb.service with below content in it.

If you try to go to URL  in your browser you will get below message.

mongodb with django

To start the MongoDB automatically on system startup, run below command.


MongoDB Shell:

Lets quickly try your hands on MongoDB shell.

MongoDB shell

Fire up the Mongo. Command is  mongo .  To see the list of available commands type help .

mongodb help
mongodb help
Show dbs  command will list the available DBs in MongoDB. Any database or collection or document is created at runtime if doesn’t exists already.


Inserting data in document:

First we need to choose a DB to work upon. Don’t worry if DB is not already created. As I said earlier, it will be created at runtime.

Now insert data in this DB. Insert statement will create a new document in the collection provided.


Fetching Data:

Use command find() over a collection to fetch all documents in that collection.


Bulk Insert:

You can create multiple documents in one go.


If you do not pass _id  key than it is automatically created.


You can search a specific document by passing the key value pair in find.

Read more about the shell operations here.

Using MongoDB in Django App:

We will be using MongoEngine which is a Document-Object Mapper for working with MongoDB from Python.

Now create a Django Project. This article explains how to start with Django. I recommend to use virtual environment. Please install mongoengine package using pip.

Complete code of Django project I have used below as example is available on Github. All the dependencies are present in req.txt file in base directory. Install all the dependencies using below command.


Complete the initial setup of your project and then complete the below steps one by one:

  • Create a new app mongoapp . Create a urls file in this app. Include this url file in project urls file.

  • Define models in mongoapp/  file.

  • Add urls in mongoapp/  file.

  • Now define functions in your mongoapp/  file.
Code above is self explanatory if you are familiar with Django.

  • Create templates in templates/mongoapp/  directory.





Congratulations! your code is ready. Restart the Django server.

Go to localhost:8000/mongoapp.

mongodb example django

If you click on show documents before creating document, then nothing will be displayed. So first click the ‘create document link’. It will execute the code in create  function of  file.

After creating document, browser redirects to show page where you can see the just created documents.

django mongodb

You can verify the same from command line.

Clicking delete link will remove the document from the MongoDB.


Dynamic Document Schema:

So far we created models with fixed schema just like relational databases. One of the benefits of MongoDB is dynamic schema. Although Explicit is better than implicit, there are few scenarios where dynamic schema is desirable.

Update mongoapp/  to add new model.

Add a new url pattern in mongoapp/  file

Add the function create_dynamic  in mongoapp/  file which will create a dynamic document.

Now navigating to the http://localhost:8000/mongoapp/create_dynamic/  url will create a new dynamic schema document. You will see the new document on show page. Same can also be verifies in terminal.

dynamic schema mongodb django


This was a basic introduction to MongoDB and how to use it in Django application. You may refer the below mentioned reference links to learn more about it.

Happy learning.




(Visited 2,371 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *