Configure Your Database Connection
On this page
Overview
In this guide, you can learn how to configure your Django project's connection to MongoDB.
Connection Configuration
After installing Django MongoDB Backend and creating a project, you can configure your connection to MongoDB in the following ways:
Manually Configure Database Settings by specifying the
DATABASES
variable in your project's settings.Automatically Configure Database Settings by using the
parse_uri()
function.
Tip
To learn how to install Django MongoDB Backend and create a Django project, visit the Get Started with Django MongoDB Backend tutorial.
Manually Configure Database Settings
To manually configure your connection to MongoDB, update
the DATABASES
variable in your project's settings.py
file. Set the DATABASES
variable to a dictionary value containing
the default
key, as shown in the following example:
DATABASES = { "default": { # Specify nested dictionary keys here }, }
To configure the default
key, assign a nested dictionary as its value.
This nested dictionary has the following keys:
Key | Description |
---|---|
ENGINE | The backend driver to use for the connection. Set this key to |
HOST | Your connection URI. For localhost connections, this key is optional. For SRV connections, you must include a scheme prefix ( mongodb+srv:// ).To specify more than one host, include all hostnames in one string. Use
a comma to separate each hostname. Example: "HOST": "mongodb://mongos0.example.com:27017,mongos1.example.com:27017" |
NAME | The database you want to use. |
USER | The username for authenticating to the database, if your connection requires authentication. |
PASSWORD | The password for your database user, if your connection requires authentication. |
PORT | The port number on which the database server is listening. The default
port is 27017 .For MongoDB Atlas connections, this key is optional. |
OPTIONS | A dictionary of additional connection options for the database. This key is optional. To see a full list of connection options that you can set in the OPTIONS key,
see the optional parameters for MongoClient
in the PyMongo API documentation. |
Example
In this example, the DATABASES
variable performs the
following actions:
Sets the database to
my_database
Provides authentication information for a database user whose username is
my_user
and password ismy_password
Specifies the default MongoDB port (
27017
)Sets the
retryWrites
connection option totrue
, which configures the driver to automatically retry certain write operations if they failSets the
w
connection option tomajority
, which configures the driver to wait for acknowledgement from a majority of replica set members before performing write operations
DATABASES = { "default": { "ENGINE": "django_mongodb_backend", "HOST": "mongodb+srv://cluster0.example.mongodb.net", "NAME": "my_database", "USER": "my_user", "PASSWORD": "my_password", "PORT": 27017, "OPTIONS": { "retryWrites": "true", "w": "majority", }, }, }
Automatically Configure Database Settings
To automatically construct the DATABASES
setting that configures
your MongoDB connection, you can use the parse_uri()
function. This
function accepts the following arguments:
uri
: Your MongoDB connection URI.conn_max_age
: Configures persistent database connections. This argument is optional. To learn more, see Persistent connections in the Django documentation.test
: Provides a dictionary of settings for test databases. This argument is optional. To learn more, see the TEST setting in the Django documentation.
Example
The following example uses the parse_uri()
function to specify
the same connection configuration as the previous manual configuration
example:
import django_mongodb_backend MONGODB_URI = "mongodb+srv://my_user:my_password@cluster0.example.mongodb.net/my_database?retryWrites=true&w=majority" DATABASES["default"] = django_mongodb_backend.parse_uri(MONGODB_URI)
Additional Information
To view a sample project that configures a MongoDB database connection, see the Configure your MongoDB Connection step in the Getting Started tutorial.
To learn more about Django settings, see Settings in the Django documentation.