DEV Community

Accessing MongoDB Atlas From Netlify Functions

Kevin Lewis on June 05, 2020

I recently tried to use a MongoDB Atlas instance with Netlify Functions and couldn't seem to connect to the database (I could locally). Here's why,...
Collapse
 
borche profile image
Andreas Börjesson

Thanks for this info

Collapse
 
coisox profile image
Aizal Manan

What!!!!!!!!!! You really really really really save my time! After a very long journey. The should have make it as default and optionally allow to specify IP for better security.

Collapse
 
iamother profile image
Andres Romaña

What if i only need access from my frontend application ?

Collapse
 
_phzn profile image
Kevin Lewis

You shouldn't be making requests directly to a database from the frontend, as it will provide folks with the credentials required to access your database.

Collapse
 
iamother profile image
Andres Romaña

So, how we show the data for the user? example fetching a table posts etc!! Do you have any docs link to read more about that !!

Thread Thread
 
_phzn profile image
Kevin Lewis

You can use a Netlify function for this. The finished flow would be something like:

  1. Your frontend makes a request to your Netlify function
  2. Your Netlify function accesses your database and retrieves posts
  3. Your Netlify functions does any necessary preparation of the data and responds to the request
  4. Your frontend receives a response from your Netlify function and can display them

Hope this helps

Collapse
 
animeshsinghweb profile image
Animesh Singh

Saved time before hunting IP addresses!

Collapse
 
_phzn profile image
Kevin Lewis

I'm glad you are saved the time I spent doing the same 🤣

Collapse
 
mral_x profile image
Alex Ferreira

Thanks for this! It would've taken waaaaaay longer than it already did for me to get to this conclusion! 🙏

Collapse
 
_phzn profile image
Kevin Lewis

You’re very welcome!

Collapse
 
permlap profile image
permlap

You really save my day!!!

Collapse
 
gillyrabuttsurwa profile image
Gilbert Rabut Tsurwa

This helped me greatly. Thank you very much.