Skip to content

Instantly share code, notes, and snippets.

@ecerami
Created March 28, 2021 12:19
Show Gist options
  • Save ecerami/5a26fff1b50a43e9654f935747221022 to your computer and use it in GitHub Desktop.
Save ecerami/5a26fff1b50a43e9654f935747221022 to your computer and use it in GitHub Desktop.
# FastAPI + MongoDB
# Option 3: Use the async Motor library.
import motor.motor_asyncio
from fastapi import FastAPI, status
import os
from typing import List
# Sample Movie Database from Atlas MongoDB
DB = "sample_mflix"
MSG_COLLECTION = "movies"
# Instantiate the FastAPI
app = FastAPI()
uri = "mongodb+srv://%s:%s@%s/sample_mflix?retryWrites=true&w=majority" % (
os.environ["MONGO_USER"],
os.environ["MONGO_PASSWORD"],
os.environ["MONGO_HOST"],
)
# Instantiate the FastAPI
app = FastAPI()
client = motor.motor_asyncio.AsyncIOMotorClient(uri)
@app.get("/movies/{genre}", response_model=List[str])
async def get_movies(genre: str):
"""Get first N movies in the specified genre."""
movie_collection = client[DB][MSG_COLLECTION]
cursor = movie_collection.find({"genres": genre}).limit(100)
movie_title_list = []
async for msg in cursor:
movie_title_list.append(msg["title"])
return movie_title_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment