Connecting to MongoDB Atlas

This guide walks through connecting your Render-hosted application to a database hosted on MongoDB Atlas. This is an alternative to hosting a containerized instance of MongoDB on Render. If you prefer to host your own MongoDB instance on Render, see Deploy MongoDB.

For advanced usage and troubleshooting, see the MongoDB documentation

Create and configure a database

You complete these steps in the MongoDB Atlas web interface.

  1. Select one of the following deployment options for your database:

    • Serverless
    • Dedicated
    • Shared
  2. Select AWS as the cloud provider and pick the AWS region closest to the region where your Render app is deployed. You can also set the cluster tier, cluster name, and any additional settings at this point. Click Create Cluster.

    Render RegionDatabase Region
    Oregon, USAOregon (us-west-2)
    Ohio, USADedicated tier: Ohio (us-east-2)
    Shared tier: N. Virginia (us-east-1)
    Virginia, USAVirginia (us-east-1)
    Frankfurt, GermanyFrankfurt (eu-central-1)
    SingaporeSingapore (ap-southeast-1)
  3. Choose an authentication method. This guide assumes you are using a username and password for authentication. You could also use a Certificate.

  4. Create a user profile for the new database and make a note of your database username and password. You will create environment variables for these values in your Render service connecting to Atlas.

  5. Update cluster connections under “Network Access”. Add your Render service’s static outbound IP addresses.

    MongoDB Atlas IP

  6. Under “Connection Method”, select “Connect your Application”. Pick the Mongo driver and version used in your Render service to create a connection string URI.

Connect to your application on Render

  1. Return to the Render Dashboard and create environment variables for username and password in your Render service using the database username and password your created above.

  2. Add connection details to your code by following the steps for your app’s language or framework.

That’s it! Your Render service should now be able to connect to your MongoDB Atlas instance.

Further reading

MongoDB supports a variety of drivers. This guide highlights some of the most useful resources for Python and Node applications.

Python

The method for adding a database connection to your Python application code depends on whether your application is synchronous or asynchronous.

Use Pymongo in your application to connect to MongoDB (for synchronous applications)

Use Motor in your Python app to connect to MongoDB Atlas (for asynchronous applications using either Tornado or asyncio.

Node