Complete the code to create an async MongoDB client using Motor.
from motor.motor_asyncio import AsyncIOMotorClient client = AsyncIOMotorClient([1]) db = client.test_database
The connection string for MongoDB must start with mongodb://. This tells Motor to connect to a MongoDB server.
Complete the FastAPI endpoint to insert a document into MongoDB asynchronously.
from fastapi import FastAPI app = FastAPI() @app.post('/add') async def add_item(item: dict): result = await db.items.[1](item) return {'inserted_id': str(result.inserted_id)}
To add a new document, use insert_one which inserts a single document asynchronously.
Fix the error in the async function to find one document by name.
async def get_item(name: str): document = await db.items.find_one({{'name': [1]) return document
The value for the 'name' key should be the variable name without quotes, so it uses the function argument.
Fill both blanks to update a document's field asynchronously.
async def update_item(name: str, new_value: str): result = await db.items.update_one( {{'name': [1], {{'$set': {{'field': [2] ) return result.modified_count
Use the variable name to find the document and new_value to set the new field value.
Fill all three blanks to delete a document by id asynchronously.
from bson import ObjectId async def delete_item(item_id: str): result = await db.items.delete_one({{'_id': [1]([2])}}) return result.deleted_count # Call example: await delete_item([3])
Use ObjectId to convert the string item_id to an ObjectId for the query. The call example uses the variable item_id.