Cannot connect to remote MongoDB server using flask-mongoengine


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

File "", line 9, in <module>
    inserted = Something(some='whatever').save()
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/mongoengine/", line 323, in save
    object_id =, **write_concern)
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/", line 2186, in save
    with self._socket_for_writes() as sock_info:
  File "/usr/lib/python3.5/", 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/", 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/", line 210, in select_server
  File "/home/lokesh/Desktop/Work/Survaider_Apps/new_survaider/survaider-env/lib/python3.5/site-packages/pymongo/", line 186, in select_servers
pymongo.errors.ServerSelectionTimeoutError: admin:27017: [Errno -2] Name or service not known

Below is the code I am using:


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()

for obj in Something.objects:

My file contains:


    'db': 'testdb',
    'host': 'mongodb://<my_username>:<my_password>@<my_cluster_replica_1>,<my_cluster_replica_2>,<my_cluster_replica_3><my_cluster>&authSource=admin',

But I can connect using pymongo using the following code.

from pymongo import MongoClient

uri = 'mongodb://<my_username>:<my_password>@<my_cluster_replica_1>,<my_cluster_replica_2>,<my_cluster_replica_3><my_cluster>&authSource=admin'

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

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

for col in db.test_collection.find():

# Prints {'some_key': 'some_value', '_id': ObjectId('57ec35d9312f911329e54d5e')}

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.


I figured out that it's a bug in flask-mongoengine version 0.8 and has beed reported here.

Even though this is a DevOps pipeline and not a production environment, what you are describing sounds like a blue/green deployment scenario in which you want to be able to switch between environments without downtime. I think the best answer is largely specific to your requirements (which we don't 100% know), but a guide like The DOs and DON'Ts of Blue/Green Deployment will be beneficial in finding the best way to achieve your goals, whether it is #1, #2, or something else.

By : jbird

I would use the following:

var startIndex = $('.start').index();
var endIndex = startIndex - 5;

for(var i = startIndex - 1; i >= endIndex; i--){

Updated to not add class 'good' the start index.

This video can help you solving your question :)
By: admin