In this guide, you can learn how to configure your Django project's connection to MongoDB.
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
setting in your project's settings.
Automatically Configure Database Settings by using the parse_uri()
function.
To manually configure your connection to MongoDB, update the DATABASES
setting in your project's settings.py
file. Set it to a dictionary containing the default
key:
DATABASES = { "default": { },}
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 "django_mongodb_backend"
.
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
MongoClientin the PyMongo API documentation.
In this example, the DATABASES
setting performs the following actions:
Sets the database to my_database
Provides authentication information for a database user whose username is my_user
and password is my_password
Specifies the default MongoDB port (27017
)
Sets the retryWrites
connection option to true
, which configures the driver to automatically retry certain write operations if they fail
Sets the w
connection option to majority
, 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", }, },}
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.
db_name
: The name of the database you want to use.
test
: Provides a dictionary of settings for test databases. This argument is optional. To learn more, see the TEST setting in the Django documentation.
The following example uses the parse_uri()
function to specify the same connection configuration as the previous manual configuration example:
import django_mongodb_backendMONGODB_URI = "mongodb+srv://my_user:my_password@cluster0.example.mongodb.net/?retryWrites=true&w=majority"DATABASES["default"] = django_mongodb_backend.parse_uri(MONGODB_URI, db_name="<database name>")
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4