Challenge - 5 Problems
MongoDB Motor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find documents with a specific field value
Given a Motor collection with documents containing a field
Assume the collection has 3 documents with
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)Attempts:
2 left
💡 Hint
Remember that
find returns a cursor for all matching documents, and to_list collects them into a list.✗ Incorrect
The
find method returns a cursor for all documents matching the filter. Using to_list with a length of 100 collects up to 100 matching documents into a list. Since there are 3 documents with status equal to "active", the result is a list of those 3 documents.📝 Syntax
intermediate1: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:
Code snippet:
await collection.update_one({"_id": id}, {"$set": {"name": "Alice"}})Attempts:
2 left
💡 Hint
Check for unmatched parentheses or extra characters.
✗ Incorrect
Option D has an extra closing parenthesis at the end, causing a syntax error. Options A and D are syntactically correct (Python allows semicolons but they are optional). Option D is missing a closing parenthesis, also a syntax error but different from C.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Think about memory usage when loading many documents.
✗ Incorrect
Option A uses an async iterator to process documents one at a time, which is memory efficient. Option A loads all documents into memory at once, which can cause high memory usage. Option A is inefficient because it makes 10,000 separate queries. Option A does not await the cursor, so it does not fetch data properly.
🔧 Debug
advanced1: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})Attempts:
2 left
💡 Hint
Remember that Motor methods return coroutines that must be awaited.
✗ Incorrect
Without
await, collection.find_one() returns a coroutine object, not the actual document. Using it directly causes a TypeError because the coroutine is not awaited.🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about connection overhead and resource management.
✗ Incorrect
Creating a single Motor client at app startup and reusing it avoids overhead of reconnecting and is efficient. Creating a client per request (A) is expensive. Closing the client after each request (B and C) defeats the purpose of connection pooling and adds overhead.