Motor#

Quart-Mongo uses Motor for general MongoDB connections.

Connection#

The :attribute:`~quart_mongo.Mongo.cx` will be an instance of AsyncIOMotorClient if the URI was passed to Mongo or set via the app configuration variable. Refer to `Configuration`_ for additional information.

Database#

The :attribute:`~quart_mongo.Mongo.db` will be an instance of AIOMotorDatabase if the database name was passed with the URI. If it was not, you will need to set the database manually like so:

mongo = Mongo(app)
mongo.db = mongo.cx['test-database']

# or
mongo.db = mongo.cx.engine('test-database')

Getting a Collection#

Once the database is setup. You can start finding documents in a collection. You get a collection by calling the :attribute:`~quart_mongo.Mongo.db` in the following manner:

mongo = Mongo(app)
mongo.db = mongo.cx['test-database']

# using dot notation
collection = mongo.db.some_collection
# or
collection = mongo.db['some_collection']

Now you can follow the Motor documentation for inserting and finding documents.

Find One or 404#

The extension provides a helper function to find a document or it will raise a 404 error.

async AIOMotorCollection.find_one_or_404(*args, **kwargs) Dict#

Find a single document or raise a 404 with the browser.

This function is like AsyncIOMotorCollection.Collection.find_one, but rather than returning None. It will raise a 404 error (Not Found HTTP status) on the request.

Parameters:
  • args – Arguments to be passed to AsyncIOMotorCollection.Collection.find_one.

  • kwargs – Extra arguments to be passed to AsyncIOMotorCollection.Collection.find_one.

Send File Helpers#

Two helper functions are provided for sending files from GridFS. They are:

async Mongo.send_file_by_name(filename: str, base: str = 'fs', version: int = -1, cache_for: int = 31536000) Response#

Respond with a file from GridFS using the file name.

Returns an instance of the response_class containing the named file, and implement conditional GET semantics.

@app.route("/uploads/<path:filename>")
async def get_upload(filename):
    return await mongo.send_file_by_name(filename)
Parameters:
  • filename – The filename of the file to return

  • base – The base name of the GridFS collections to use

  • version – If positive, return the Nth revision of the file identified by filename; if negative, return the Nth most recent revision. If no such version exists, return with HTTP status 404.

  • cache_for (int) – Number of seconds that browsers should be instructed to cache responses

async Mongo.send_file_by_id(id: ObjectId, base: str = 'fs', version: int = -1, cache_for: int = 31536000) Response#

Respond with a file from GridFS using the file Object id.

Returns an instance of the response_class containing the named file, and implement conditional GET semantics.

@app.route("/uploads/<str:id>")
async def get_upload(id):
    return await mongo.send_file_by_id(id)
Parameters:
  • id – The bson Object id of the file.

  • base – The base name of the GridFS collections to use

  • version – If positive, return the Nth revision of the file identified by filename; if negative, return the Nth most recent revision. If no such version exists, return with HTTP status 404.

  • cache_for (int) – Number of seconds that browsers should be instructed to cache responses

Save File Helper#

A helper function is provided for saving files to GridFS.

async Mongo.save_file(filename: str, fileobj: SupportsRead[AnyStr], base: str = 'fs', content_type: str | None = None, **kwargs) ObjectId#

Save a file-like object to GridFS using the given filename.

@app.route("/uploads/<path:filename>", methods=["POST"])
async def save_upload(filename):
    await mongo.save_file(filename, request.files["file"])
    return redirect(url_for("get_upload", filename=filename))
Parameters:
  • filename – the filename of the file to return

  • fileobj (file) – the file-like object to save

  • base – base the base name of the GridFS collections to use

  • content_type – the MIME content-type of the file. If None, the content-type is guessed from the filename using guess_type()

  • kwargs – extra attributes to be stored in the file’s document,

:param passed directly to gridfs.GridFS.put():