DEV Community

Cover image for Trying Out MongoDB database Backend for Django
c a
c a

Posted on

Trying Out MongoDB database Backend for Django

I know many developers who use Django, of course with Relational Databases: Postgres, SQL, SQLite.

Getting the news from MongoDB that the Django MongoDB Backend is now in Public Preview! was crazy!!! and Amazing of course.

So I decided to give it a try. I made some rookie mistakes and assumptions(😂😂)


Initially I thought it would be a simple plug and play configuration, so I started out by testing it in already existing Django project, in this case, my starter template.


Creating a MongoDB Account & a Cluster

I have used MongoDB on most javascript-based apps, so I know my around it.

You can learn more about creating a cluster here.

A free cluster should work for the testing:

screenshot of creating mongo cluster

After that, you will need to create users to access the database, once that is done, just give it a few minutes for the cluster to build up.

screenshot of creating mongo cluster

Once that is done, get the connection string, that looks like this:

mongodb+srv://chrisdevcode:<db_password>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo
Enter fullscreen mode Exit fullscreen mode

So I cloned the repo, installed the initial dependencies and add install the django-mongodb-backend package:

pip install django-mongodb-backend
Enter fullscreen mode Exit fullscreen mode

Then of course update the DATABASE settings to use MongoDB:

DATABASES = {
  "default": django_mongodb_backend.parse_uri("<connection string URI>"),
}
Enter fullscreen mode Exit fullscreen mode

Looked easy to me, till I ran migrations:

migration errors

Well, that did go well ...

disappointed

So back to the article by MongoDB's team:

I asked if one has to start from their starter template:

Thanks for your question @chrisachinga!

We recommend doing so to ensure certain necessities are incorporated, such as MongoDB-specific migrations and having the settings.py file already modified for you so Django is using an ObjectId value.

Let me know if you have any other questions, I'd be happy to help.

That was helpful at the moment, oh and don't use it in production ... yet

Thanks for your reply @chrisachinga,

So we don't recommend using our library in production until our GA.
For any issues please open a GitHub issue or Jira ticket!

We appreciate your feedback so much. :)

So we try using the MongoDB starter …

Following the docs:

https://www.mongodb.com/docs/languages/python/django-mongodb/current/

So you create a django project from the template:

django-admin startproject quickstart --template https://github.com/mongodb-labs/django-mongodb-project/archive/refs/heads/5.1.x.zip
Enter fullscreen mode Exit fullscreen mode

Then you update the connection string:

DATABASES = {
   "default": django_mongodb_backend.parse_uri("<connection string URI>", db_name="<database name>"),
}
Enter fullscreen mode Exit fullscreen mode

You also create an application from a starter template:

python manage.py startapp sample_mflix --template https://github.com/mongodb-labs/django-mongodb-app/archive/refs/heads/5.1.x.zip
Enter fullscreen mode Exit fullscreen mode

After that, I ran my migrations:

database name error

The error was about the DATABASE setting missing the "NAME" value, so I thought this would fix it:

DATABASES = {
    "name": "default",
    "default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<password>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo"),
}
Enter fullscreen mode Exit fullscreen mode

So that should work, right?

wrong

Confusing right? Maybe I rushed into the fix. So I asked on the forum:

MongoDB Django Database Backend - Python Frameworks - MongoDB Developer Community Forums

[https://jira.mongodb.org/browse/INTPYTHON-535](https://JIRA ISSUE CREATED) PS: I tried using the mongodb backend to an ongoing django project, but was advised to get started from the template(at least for now). I still want to try the mongodb backend and do some benchmarks on performance as compared to postgres. I got these two issues, maybe the team can help address, but I’m pretty sure I may have missed a step or something from the official guideline(feel free to criticize my feedback) set...

favicon mongodb.com

Jeffery A. Clark responded with a couple of fixes:

MongoDB Django Database Backend - #2 by Alex_Clark - Python Frameworks - MongoDB Developer Community Forums

Thank you for using Django MongoDB Backend! Try including the database name in the URI: DATABASES = { "default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/default?retryWrites=true&w=majority&appName=djangomongo"), } Or try configuring the NAME setting like this: DATABASES = { "default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomo...

favicon mongodb.com

DATABASES = {
    "default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo"),
}

DATABASES["default"]["NAME"] = "default"
Enter fullscreen mode Exit fullscreen mode

or

(will be available on beta 1)

DATABASES = {
    "default": django_mongodb_backend.parse_uri("mongodb+srv://chrisdevcode:<pass>@djangomongo.oybyx.mongodb.net/?retryWrites=true&w=majority&appName=djangomongo", db_name="default"),
}
Enter fullscreen mode Exit fullscreen mode

The fix is well explained on the PR: https://github.com/mongodb/django-mongodb-backend/pull/249

Did that work? Yes ...

cluster images

What To Do next?

Testing and comparing performances with other database backends.

What I love about this: Deployment will be super easy and quick, especially that connection to MongoDB is by a URI, and hopefully no other stuff needs to be done ... trying that next 👀👀

This podcast on Django Chat talks about the mongodb database backend on django:

Official Django MongoDB Backen–Django Chat – Apple Podcasts

Podcast Episode · Django Chat · 19/02/2025 · 1h 3m

favicon podcasts.apple.com

Also another good read:

Top comments (1)

Collapse
 
anaiyaraisin profile image
Anaiya

Glad you tried it out for your self! Happy to see you got it working, let me know if you need any more help or direction.