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
You complete these steps in the MongoDB Atlas web interface.
Select one of the following deployment options for your database:
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 Region Database Region
N. Virginia (us-east-1)
Choose an authentication method. This guide assumes you are using a username and password for authentication. You could also use a Certificate.
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.
Update cluster connections under “Network Access”. Add your Render service’s static outbound IP addresses.
Under “Connection Method”, select “Connect your Application”. Pick the Mongo driver and version used in your Render service to create a connection string URI.
Return to the Render Dashboard and create environment variables for
passwordin your Render service using the database username and password your created above.
- There are some characters that require special treatment. See the MongoDB documentation on connection string formats
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.
MongoDB supports a variety of drivers. This guide highlights some of the most useful resources for Python and Node applications.
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.
- Tutorial for completing common database operations using Motor with Tornado
- Tutorial for completing common database operations using Motor with asyncio