Skip to content

Rate this page
Thanks for your feedback
Thank you! The feedback has been submitted.

Get free database assistance or contact our experts for personalized support.

App configuration

A few practices help keep your application secure and easy to run in different environments.

Use environment variables for the connection string

Do not hardcode the MongoDB URI or credentials in your code. Use environment variables (for example MONGODB_URI or MONGODB_USER and MONGODB_PASSWORD) so you can change them per environment (local, staging, production) without changing code.

Example:

export MONGODB_URI="mongodb://myuser:mypassword@my-cluster-mongos.default.svc.cluster.local/admin?ssl=false"

Your app reads the variable at startup. In Kubernetes, you can inject it from a Secret or ConfigMap.

Use a dedicated application user

Use an application-level (unprivileged) user for your app instead of the database admin account. Create one user per application or service, with only the roles it needs (for example readWrite on a single database). That limits the impact of leaked credentials and follows least-privilege.

Optional: retries and connection pooling

MongoDB drivers support automatic retries and connection pooling. For replica sets, the driver can reconnect and fail over if the primary changes. See your driver’s documentation (for example Node.js, Python, Go) for options like connection timeouts and retry logic.

Next steps


Last update: March 23, 2026
Created: March 23, 2026