Get the MongoDB Connection String

Open your Atlas database deployment and select Connect.

Choose the Drivers connection option.

Select Node.js and copy the connection string.

It generally looks like this:

mongodb+srv://<username>:<password>@cluster.example.mongodb.net/<database>

Replace the placeholders with your database username, password, and database name.

Special characters in database credentials must be percent-encoded when embedded in a MongoDB URI.

Add the resulting connection string to the MONGODB_URI environment variable.

Never expose this connection string to the browser.

.env
MONGODB_URI=mongodb+srv://dbuser:encoded-password@cluster.example.mongodb.net/rest_api_db

Stores the MongoDB connection string in a backend environment variable. Replace the example hostname and credentials with your actual Atlas connection details.

Key Takeaways
  • Copy the connection string from the Atlas Drivers section.

  • Use the database username and password.

  • Encode special characters in credentials.

  • Keep the connection string on the backend.