MongoDB Connection

Connect your Dart application to a MongoDB database

Setting Up MongoDB Connection

Home documentation

To successfully connect your Dart application to a MongoDB database, you need to configure several settings in your .env file. Below are the necessary configurations and their explanations:

1. .env File Configuration

Add the following entries to your .env file:

# MONGO DATABASE Configuration
MONGO_INITDB_ROOT_USERNAME=db_username
MONGO_INITDB_ROOT_PASSWORD=db_password
MONGO_CONNECTION=mongo
MONGO_PORT=27017
NODE_ENV=production
MONGO_INITDB_DATABASE=db_database_name

Explanation of Each Setting:

  • MONGO_INITDB_ROOT_USERNAME:
    • Description: The root username for your MongoDB instance. This is the username used to authenticate and access the MongoDB database.
    • Example: db_username
  • MONGO_INITDB_ROOT_PASSWORD:
    • Description: The root password associated with the MongoDB root username. This password, along with the username, is required to connect to the database.
    • Example: db_password
  • MONGO_CONNECTION:
    • Description: The hostname or IP address where your MongoDB instance is running. This could be localhost if the database is running locally, or mongo if it’s running in a Docker container linked to your application.
    • Example: mongo
  • MONGO_PORT:
    • Description: The port number on which your MongoDB instance is listening. By default, MongoDB listens on port 27017.
    • Example: 27017
  • NODE_ENV:
    • Description: The environment in which your application is running. Common values are development, production, and test.
    • Example: production
  • MONGO_INITDB_DATABASE:
    • Description: The name of the initial database to use when connecting to MongoDB. This is the database that your application will interact with.
    • Example: db_database_name

2. Enabling Database Configuration in Your Dart Application

To enable MongoDB configuration in your Dart application, you need to modify the WaConfigs object in your code. Specifically, you should set the enable property of dbConfig to true. This will activate the MongoDB connection using the settings specified in the .env file.

Code Example:

WaConfigs configs = WaConfigs(
  widgetsPath: pathTo("./example/widgets"),
  widgetsType: 'j2.html',
  languagePath: pathTo('./example/languages'),
  port: 8085,
  dbConfig: WaDBConfig(enable: true), // Set to true to enable MongoDB connection
  publicDir: pathTo('./example/public'),
);

Configuring MongoDB Connection Without Using .env File

If you prefer to configure your MongoDB connection directly in your Dart code without relying on a .env file, you can do so by specifying the necessary database connection details directly within the WaConfigs object.

Code Example:

WaConfigs configs = WaConfigs(
  widgetsPath: pathTo("./example/widgets"),
  widgetsType: 'j2.html',
  languagePath: pathTo('./example/languages'),
  port: 8085,
  dbConfig: WaDBConfig(
    user: 'db_username',
    pass: 'w5f7ewefwefuweif24',
    host: 'example.com',
    port: '27017',
    dbName: 'database_name',
    auth: 'admin', // Authenticator
    enable: true,
  ), 
  publicDir: pathTo('./example/public'),
);

Explanation of Each Parameter:

  • user:
    • Description: The username used to authenticate with the MongoDB database.
    • Example: 'db_username'
  • pass:
    • Description: The password associated with the username. This is used to securely authenticate your connection to the database.
    • Example: 'w5f7ewefwefuweif24'
  • host:
    • Description: The hostname or IP address where your MongoDB instance is hosted. This could be an external domain, a local IP address, or localhost if running locally.
    • Example: 'example.com'
  • port:
    • Description: The port number on which MongoDB is listening. Typically, MongoDB runs on port 27017.
    • Example: '27017'
  • dbName:
    • Description: The name of the database to connect to within MongoDB.
    • Example: 'database_name'
  • auth:
    • Description: Specifies the authentication mechanism to be used. This is often set to 'admin', indicating that the authentication is performed using the admin database.
    • Example: 'admin'
  • enable:
    • Description: A boolean flag that enables or disables the MongoDB connection. Set this to true to activate the connection.
    • Example: true

Summary

By following the steps above, you configure your application to connect to a MongoDB database using the credentials and connection details specified in the .env file. Remember to set dbConfig to true in your WaConfigs to ensure that your application attempts to connect to the MongoDB instance.