title: MongoEngine - Another user is already authenticated to this database. You must logout first
body: <p>Can anyone please explain why I am getting error <strong>Another user is already authenticated to this database. You must logout first</strong> when connecting to MongoDB using <a href="https://github.com/MongoEngine/flask-mongoengine" rel="noreferrer">Flask MongoEngine</a>?</p>
  
  <pre><code>from mongoengine.connection import get_db
  from flask import Flask, jsonify, abort
  from flask_cors import CORS
  from flask_mongoengine import MongoEngine
  from flask_restful import Api
  
  def init_db():
      return MongoEngine()
  
  app = Flask(__name__)
  CORS(app)
  api = Api(app)
  app.config.from_object('conf.settings')
  db = init_db()
  db.init_app(app)
  
  @app.route('/health_check')
  def on_health_check():
      try:
          db = get_db()
          db.command('dbstats')
  
          return jsonify(
              status=200
          )
      except Exception as e:
          logging.exception('on_health_check() exception -&gt; {}'.format(str(e)))
          abort(500, 'Could not connect to database')
  
  
  app.run(host='0.0.0.0', port=5000, debug=True, threaded=True)
  </code></pre>
  
  <p>conf/settings.py:</p>
  
  <pre><code>MONGODB_SETTINGS = {
      'host': 'mongodb://username:password@mongo-rep-mongodb-replicaset-0.local:27017,mongo-rep-mongodb-replicaset-1.local:27017/db_name?replicaSet=whatever'
  }
  </code></pre>
  
  <p>When I go to <code>http://localhost:5000/health_check</code>, it always throws the <code>Exception</code> with message as I described above.</p>