How to connect with atlas using MongoEngine?

I am trying to connect with MongoDB atlas from my flask app using flask-mongoengine.

DB_URI =  "mongodb+srv://flask_app_user:flask_app_user@cluster0.6jwadcx5g.mongodb.net/flask_app?retryWrites=true&w=majority"

def create_app():
    app = Flask(__name__)
    app.secret_key = os.environ.get('SECRET_KEY', 'replace_me_32437264278642')
    app.config['MONGODB_SETTINGS'] = {
        'host': os.environ.get('MONGODB_URI', DB_URI)
    }
    MongoEngine(app)
    socketio.init_app(app)
    SSLify(app)

    return app

But I am getting an error,

pymongo.errors.InvalidURI: Invalid URI scheme: URI must begin with 'mongodb://'

How can I use mongo atlas with flask_mongoengine? I don’t want to stick with flask_mongoengine. I don’t want to change that.

Hi @Tanush_Software,

Welcome to MongoDB community.

I would recommend trying a standard MongoDB connection starting mongodb:// .

You can retrieve this one by using the connect tab and choosing an old driver version or shell , once you find the string using mongodb:// use it and fill in the placeholders required to fit your db and user.

Thanks
Pavel

Upgrading to pymongo>=3.6 should resolve this issue:

python -m pip install --upgrade 'pymongo>=3.6'
2 Likes