Cannot connect to remote MongoDB server using flask-mongoengine
<p>Trying to connect to a MongoDB cluster hosted on a remote server using flask-mongoengine but the following error is thrown:</p>

<pre><code>File "test.py", line 9, in &lt;module&gt;
    inserted = Something(some='whatever').save()
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/mongoengine/document.py", line 323, in save
    object_id = collection.save(doc, **write_concern)
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/collection.py", line 2186, in save
    with self._socket_for_writes() as sock_info:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/mongo_client.py", line 762, in _get_socket
    server = self._get_topology().select_server(selector)
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/topology.py", line 210, in select_server
    address))
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/topology.py", line 186, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: admin:27017: [Errno -2] Name or service not known
</code></pre>

<p>Below is the code I am using:</p>

<pre><code># test.py

from my_app_module import app
from flask_mongoengine import MongoEngine

db = MongoEngine(app)

class Something(db.Document):
    some = db.StringField()

inserted = Something(some='whatever').save()

print(inserted)
for obj in Something.objects:
    print(obj)
</code></pre>

<p>My <code>config.py</code> file contains:</p>

<pre><code># config.py

MONGODB_SETTINGS = {
    'db': 'testdb',
    'host': 'mongodb://&lt;my_username&gt;:&lt;my_password&gt;@&lt;my_cluster_replica_1&gt;.mongodb.net:27017,&lt;my_cluster_replica_2&gt;.mongodb.net:27017,&lt;my_cluster_replica_3&gt;.mongodb.net:27017/admin?ssl=true&amp;replicaSet=&lt;my_cluster&gt;&amp;authSource=admin',
}
</code></pre>

<p>But I can connect using <code>pymongo</code> using the following code.</p>

<pre><code>from pymongo import MongoClient

uri = 'mongodb://&lt;my_username&gt;:&lt;my_password&gt;@&lt;my_cluster_replica_1&gt;.mongodb.net:27017,&lt;my_cluster_replica_2&gt;.mongodb.net:27017,&lt;my_cluster_replica_3&gt;.mongodb.net:27017/admin?ssl=true&amp;replicaSet=&lt;my_cluster&gt;&amp;authSource=admin'

client = MongoClient(uri)
db = client['testdb']

db.test_collection.insert({'some_key': 'some_value'})

for col in db.test_collection.find():
    print(col)

# Prints {'some_key': 'some_value', '_id': ObjectId('57ec35d9312f911329e54d5e')}
</code></pre>

<p>I tried to find a solution but nobody seems to have come across the problem before. I am using MongoDB's Atlas solution to host the MongoDB cluster.</p>

<p>I figured out that it's a bug in <a href="https://github.com/MongoEngine/flask-mongoengine" rel="nofollow">flask-mongoengine</a> version <code>0.8</code> and has beed reported <a href="https://github.com/MongoEngine/flask-mongoengine/issues/247" rel="nofollow">here</a>.</p>

python mongodb pymongo mongoengine flask-mongoengine