Connecting to MongoDB Atlas

Hi Anvilistas,

I want to create a server module to handle data access to a MongoDB Atlas Cluster instance.
I have defined the documents as classes in a client module, which I import in the servermodule.

I’m able to connect using following code (in the client module):

HOST = ‘mongodb+srv://username:password@cluster0.k1u9ph2.mongodb.net/?retryWrites=true&w=majority’
DBASE = ‘Cluster0’
connect(DBASE,host=HOST)
print (‘Connected to {} : {}’.format(DBASE, HOST))

This seems to work.

What I’ve tried and what’s not working:

Next i’m trying to do a query to a document in MongoDB Atlas and there I receive a timeout.
Please look at the servermodule code:

@anvil.server.callable
def m_get_personas(**query):
session = get_session(‘m_get_personas’)
result = []
personas = Personas.objects(**query) # Personas is a Document class defined in the client module
(which is imported) and here the code hangs…

for persona in personas:
    result.append({'name': persona.name,
                   'is_person': persona.is_person,
                   })
return result

Anyone experienced something similar ?
Any solution ?

Thanks

Paul

This is most likely due to a network permission issue on the atlas side.

Try to set network access to “allow from everywhere” for a few minutes.
If that solves the issue you need to add anvils egress IP’s to your allowed IP Adressed.
You need to ask support for the specific IP adresses though.

I actually wrote the skeleton for a tutorial on this before I left, but it seems it never got fleshed out - and yes, this is the exact answer

Thanks all for the feedback.
Highly appreciated :+1: