0
0
FastAPIframework~20 mins

MongoDB integration with Motor in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MongoDB Motor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with a specific field value
Given a Motor collection with documents containing a field status, what will be the output of this query?

cursor = collection.find({"status": "active"})
results = await cursor.to_list(length=100)


Assume the collection has 3 documents with status set to "active".
FastAPI
cursor = collection.find({"status": "active"})
results = await cursor.to_list(length=100)
AAn empty list because <code>to_list</code> requires a callback
BA list of all documents in the collection regardless of <code>status</code>
CA list of 3 documents where each document has <code>status</code> equal to <code>"active"</code>
DA single document with <code>status</code> equal to <code>"active"</code>
Attempts:
2 left
💡 Hint
Remember that find returns a cursor for all matching documents, and to_list collects them into a list.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in Motor query
Which option contains a syntax error when trying to update a document using Motor in FastAPI?

Code snippet:
await collection.update_one({"_id": id}, {"$set": {"name": "Alice"}})
Aawait collection.update_one({"_id": id}, {"$set": {"name": "Alice"}})
Bawait collection.update_one({"_id": id}, {"$set": {"name": "Alice"}});
Cawait collection.update_one({"_id": id}, {"$set": {"name": "Alice"}}
Dawait collection.update_one({"_id": id}, {"$set": {"name": "Alice"}}))
Attempts:
2 left
💡 Hint
Check for unmatched parentheses or extra characters.
optimization
advanced
2:30remaining
Optimize fetching large data sets with Motor
You want to fetch 10,000 documents from a Motor collection efficiently. Which approach is best to avoid high memory usage?
AUse <code>async for doc in collection.find():</code> to process documents one by one.
BUse <code>await collection.find().to_list(length=10000)</code> to get all documents at once.
CUse <code>await collection.find_one()</code> in a loop 10,000 times.
DUse <code>collection.find().limit(10000)</code> without awaiting.
Attempts:
2 left
💡 Hint
Think about memory usage when loading many documents.
🔧 Debug
advanced
1:30remaining
Debug missing await in Motor query
What error will occur if you forget to use await when calling collection.find_one() in an async FastAPI route?
FastAPI
result = collection.find_one({"_id": id})
ASyntaxError: missing await keyword
BTypeError: object coroutine can't be used directly
CRuntimeError: event loop is closed
DNo error, result contains the document
Attempts:
2 left
💡 Hint
Remember that Motor methods return coroutines that must be awaited.
🧠 Conceptual
expert
3:00remaining
Understanding Motor client connection lifecycle
In a FastAPI app using Motor, what is the best practice to manage the MongoDB client connection to avoid creating multiple clients on each request?
ACreate a single Motor client instance at app startup and reuse it for all requests.
BCreate a Motor client inside a dependency with <code>yield</code> and close it after each request.
CCreate a Motor client globally but close it after each request.
DCreate a new Motor client inside each route handler function.
Attempts:
2 left
💡 Hint
Think about connection overhead and resource management.