Configuration#

You can configure Quart-Mongo either by passing a MongoDB URI to the Mongo constructor, or assigning it to the MONGO_URI Quart configuration variable

The Mongo instance also accepts these additional customization options:

  • json_options, a JSONOptions instance which controls the JSON serialization of MongoDB objects when used with jsonify(). Also, json_options can be assigned using the configuration variable MONGO_JSON_OPTIONS.

You may also pass additional keyword arguments to the Mongo constructor. These are passed directly through to the underlying AsyncIOMotorClient object.

Note

By default, Quart-Mongo sets the connect keyword argument to False, to prevent Motor from connecting immediately. Motor itself is not fork-safe, and delaying connection until the app is actually used is necessary to avoid issues. If you wish to change this default behavior, pass connect=True as a keyword argument to Mongo.

You can create multiple Mongo instances, to connect to multiple databases or database servers:

app = Quart(__name__)

# connect to MongoDB with the defaults
mongo1 = Mongo(app, uri="mongodb://localhost:27017/databaseOne")

# connect to another MongoDB database on the same host
mongo2 = Mongo(app, uri="mongodb://localhost:27017/databaseTwo")

# connect to another MongoDB server altogether
mongo3 = Mongo(app, uri="mongodb://another.host:27017/databaseThree")

Each instance is independent of the others and shares no state.