'Connecting to mongo cloud using mongoengine

I have written a web app in django using mongoengine as a driver and am trying to host my database on mongodb cloud. In mongoengine's connect() method, according to what I understood from the documentation, specifying the url (specified in the connect section on the mongo cloud site) in the hostname argument should do the trick, however requests from the app to the mongo cloud keep timing out.

Where am I going wrong?

This is the call to the connect method in my setting.py file-

mongoengine.connect(db='scheduler', host='mongodb+srv://<user>:<password>@cluster0.oualt.mongodb.net/<db name>?retryWrites=true&w=majority')

Update: the specific error thrown on using the above url is this-

ServerSelectionTimeoutError


Solution 1:[1]

If you're using just mongoengine then Django will not detect that connect to the database, so you will not be able to use the Django ORM system to save instances of models etc.

If you want to use the ORM you would have to use a package like Djongo or djmongoengine

However, if you're not planning to use the ORM system then you can use just mongoengine. I believe the connect url should be in this format:

mongodb+srv://<username>:<password>@<database-name>.mongodb.net/test?retryWrites=true&w=majority

Right now you seem to have the url to a specific cluster on your database.

Solution 2:[2]

So I was going through similar posts which had the same error ServerSelectionTimeoutError with djongo and pymongo as the drivers and one of the posts said that they had forgotten to whitelist their IP address on the mongo cloud site. I had done that during signup, but I repeated it anyways and for some reason the IP address was different and after that had been whitelisted, my app connected and started working. I have no explanation.

Solution 3:[3]

mongoengine works fine, but in terms of protocols you need another ext file that serves for your connection protocols, just pip install pymongo[srv]

def register_database(db_name:str):
  connect(db=f"mongodb+srv://<username>:<password>@<cluster-name>.nwr5l.mongodb.net/{db_name}?retryWrites=true&w=majority", alias="default")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Felix Eklöf
Solution 2 YashSonawane
Solution 3 Marvin