Quickstart#
Add a Mongo
to your code:
from quart import Quart
from quart_mongo import Mongo
app = Quart(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/myDatabase"
mongo = Mongo(app)
Mongo
connects to the MongoDB server running on
port 27017 on localhost, to the database named myDatabase
. This database
is exposed as the db
attribute.
You can use db
directly in views:
@app.route("/")
async def home_page():
online_users = await mongo.db.users.find({"online": True})
return await render_template("index.html",
online_users=online_users)
Note
If there is no database name, the db
attribute will be None
.
If you wish to use Odmantic models to insert, get, update, and delete
collections. Then the database will also be exposed as the
odm
attribute.
You can also use odm
directly in views:
Note
If there is no database name, the odm
attribute will be None
just like ~quart_mongo.Mongo.db.