How to Connect MongoDB Atlas to Vercel Using the New Integration
Rate this quickstart
Getting a MongoDB Atlas database created and linked to your Vercel project has never been easier. In this tutorial, we’ll get everything set up—including a starter Next.js application—in just minutes.
For this tutorial, you’ll need:
This tutorial will work with any frontend framework if Next.js isn’t your preference.
Vercel is a cloud platform for hosting websites and web applications. Deployment is seamless and scaling is automatic.
For our example, we are going to use Next.js. However, you can use any web framework you would like.
We’ll use the
with-mongodbexample app to get us started. This will give us a Next.js app with MongoDB Atlas integration already set up for us.
We are using the standard
npx create-next-appcommand along with the
--example with-mongodbparameter which will give us our fully integrated MongoDB application. Then,
vercel-demois the name of our application. You can name yours whatever you would like.
After that completes, navigate to your application directory:
At this point, we need to configure our MongoDB Atlas database connection. Instead of manually setting up our database and connection within the MongoDB Atlas dashboard, we are going to do it all through Vercel!
Before we move over to Vercel, let’s publish this project to GitHub. Using the built-in Version Control within VS Code, if you are logged into your GitHub account, it’s as easy as pressing one button in the Source Control tab.
I’m going to press Publish Branch and name my new repo
Choose the repo that you just created, and then click Deploy. This deployment will actually fail because we have not set up our MongoDB integration yet.
Click Add Integration, select your account from the dropdown, and then click continue.
Next, you can either add this integration to all projects or select a specific project. I’m going to select the project I just created, and then click Add Integration.
If you do not already have a MongoDB Atlas account, you can sign up for one at this step. If you already have one, click “Log in now.”
The next step will allow you to select which Atlas Organization you would like to connect to Vercel. Either create a new one or select an existing one. Click Continue, and then I Acknowledge.
The final step allows you to select an Atlas Project and Cluster to connect to. Again, you can either create new ones or select existing ones.
After you have completed those steps, you should end up back in Vercel and see that the MongoDB integration has been completed.
If you go to your project in Vercel, then select the Environment Variables section of the Settings page, you’ll see that there is a new variable called
MONGODB_URI. This can now be used in our Next.js application.
All we have to do now is sync our environment variables to our local environment.
You can either manually copy/paste your
MONGODB_URIinto your local
.envfile, or you can use the Vercel CLI to automate that.
Let’s add the Vercel CLI to our project by running the following command:
In order to link our local project with our Vercel project, run the following command:
Choose a login method and use the browser pop-up to authenticate.
Answer yes to set up and deploy.
Select the appropriate scope for your project.
When asked to link to an existing project, type Y and press enter.
Now, type the name of your Vercel project. This will link the local project and run another build. Notice that this build works. That is because the environment variable with our MongoDB connection string is already in production.
But if you run the project locally, you will get an error message.
We need to pull the environment variables into our project. To do that, run the following:
Now, every time you update your repo, Vercel will automatically redeploy your changes to production!
In this tutorial, we set up a Vercel project, linked it to a MongoDB Atlas project and cluster, and linked our local environment to these.
These same steps will work with any framework and will provide you with the local and production environment variables you need to connect to your MongoDB Atlas database.