Cannot connect to remote MongoDB server using flask-mongoengine

Question!

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

File "test.py", 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/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

Below is the code I am using:

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

My config.py file contains:

# config.py

MONGODB_SETTINGS = {
    'db': 'testdb',
    'host': 'mongodb://<my_username>:<my_password>@<my_cluster_replica_1>.mongodb.net:27017,<my_cluster_replica_2>.mongodb.net:27017,<my_cluster_replica_3>.mongodb.net:27017/admin?ssl=true&replicaSet=<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>.mongodb.net:27017,<my_cluster_replica_2>.mongodb.net:27017,<my_cluster_replica_3>.mongodb.net:27017/admin?ssl=true&replicaSet=<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():
    print(col)

# 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.



Answers

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--){
    $('.item').eq(i).addClass('good');
}

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

https://jsfiddle.net/n2723fmu/



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