Skip to content

Instantly share code, notes, and snippets.

@singhayushh
Last active February 3, 2024 15:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save singhayushh/426f10353a8051593828e92c139ebdbc to your computer and use it in GitHub Desktop.
Save singhayushh/426f10353a8051593828e92c139ebdbc to your computer and use it in GitHub Desktop.
Connecting MongoDB Atlas

Here's how to create a free cluster and connect it to your project using the connection string

  1. Create an account on MongoDB Cloud Atlas
  2. You will be prompted right away to create a new project and org. You may leave all values as default and click Continue.
  3. Three cards will show up next asking you to choose a cluster. Select the Shared Cluster - that's the one which is free.
  4. The next screen is about details of the cluster. Make sure M0 Cluster Tier is selected. You may leave all values as default and click on Create Cluster
  5. Wait for some time for the cluster to get ready and then click on Connect.
  6. First thing you need to add it IP Address. Select Add your current IP Address if you are working on the project yourself only or Allow Access from Anywhere. You may even manually add IP Addresses by clicking on Add a different IP...
  7. Next up is Database user creation. You need to enter a username and password. For explanatory purposes, say I added them as admin and pass respectively.
  8. Next step asks you for a connection method. Select Connect Your Application and then copy the connection string that gets prompted and click close. You may even close your browser as you do not need to do anything further through the website.
  9. Now open your project and create a .env file in the directory of your backend application and add a key value pair as follows: MONGO_URI=<paste your connection string that you copied>

The string looks like this: mongodb+srv://admin:<password>@cluster0.bqss1.mongodb.net/<dbname>?retryWrites=true&w=majority

In the connection string as shown above, you need to replace <password> with the password you had created in step 7 and <dbname> with a name that you would like to name your database.

So it would finally look like this for me (with username: admin, password: pass, dbname: todo) mongodb+srv://admin:pass@cluster0.bqss1.mongodb.net/todo?retryWrites=true&w=majority

@xeny344
Copy link

xeny344 commented Jun 22, 2023

Do i Upload it to github?

@singhayushh
Copy link
Author

@zcy764 use environment variables to store secrets like mongo uri and make use of gitignore to prevent them getting pushed to GitHub.

Lookup dotenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment