Bird
0
0

Given this FastAPI async function using Motor, what will be the output if the user is found?

medium📝 query result Q4 of 15
FastAPI - Database Integration
Given this FastAPI async function using Motor, what will be the output if the user is found?
async def get_user(db, user_id):
    user = await db.users.find_one({"_id": user_id})
    return user
AA list of all users
BA dictionary with user data
CAn error because find_one is synchronous
DNone always
Step-by-Step Solution
Solution:
  1. Step 1: Understand find_one behavior

    Motor's find_one returns a dictionary of the first matching document asynchronously.
  2. Step 2: Analyze function output

    If user_id matches a document, the function returns that user dictionary; otherwise, None.
  3. Final Answer:

    A dictionary with user data -> Option B
  4. Quick Check:

    find_one returns dict or None = dict if found [OK]
Quick Trick: find_one returns dict if found, else None [OK]
Common Mistakes:
MISTAKES
  • Assuming find_one returns a list
  • Thinking find_one is synchronous
  • Expecting None always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes