Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to bind the Azure Function to Cosmos DB input.
Azure
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, [CosmosDB(databaseName: "MyDb", collectionName: "MyCollection", ConnectionStringSetting = [1], Id = "1", PartitionKey = "partitionKey")] MyDocument doc, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); return new OkObjectResult(doc); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using storage account connection string instead of Cosmos DB connection string.
Using incorrect or missing connection string setting name.
✗ Incorrect
The ConnectionStringSetting must match the app setting name for Cosmos DB connection, commonly 'CosmosDBConnection'.
2fill in blank
mediumComplete the code to output a document to Cosmos DB from the Azure Function.
Azure
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, [CosmosDB(databaseName: "MyDb", collectionName: "MyCollection", ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector<MyDocument> documentsOut, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); var inputDoc = JsonConvert.DeserializeObject<MyDocument>(requestBody); await documentsOut.[1](inputDoc); return new OkResult(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous Add method which does not exist.
Using incorrect method names like WriteAsync or Insert.
✗ Incorrect
The IAsyncCollector interface uses AddAsync to add documents asynchronously to Cosmos DB.
3fill in blank
hardFix the error in the Cosmos DB output binding attribute to specify the partition key.
Azure
[CosmosDB(databaseName: "MyDb", collectionName: "MyCollection", ConnectionStringSetting = "CosmosDBConnection", [1] = "partitionKey")] IAsyncCollector<MyDocument> documentsOut
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like PartitionKeyPath or PartitionKeyValue.
Omitting the partition key when required.
✗ Incorrect
The correct property name for specifying the partition key in the CosmosDB attribute is 'PartitionKey'.
4fill in blank
hardFill both blanks to correctly read multiple documents from Cosmos DB using a SQL query.
Azure
public static IEnumerable<MyDocument> Run([CosmosDB(databaseName: "MyDb", collectionName: "MyCollection", ConnectionStringSetting = "CosmosDBConnection", SqlQuery = [1])] IEnumerable<MyDocument> documents, ILogger log) { log.LogInformation("Documents retrieved."); return documents.Where(doc => doc.[2] > 10); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a SQL query that filters documents already, then filtering again in code.
Using a string property like 'status' in a numeric comparison.
✗ Incorrect
The SQL query selects all documents, and the code filters documents where 'score' is greater than 10.
5fill in blank
hardFill all three blanks to correctly define a Cosmos DB trigger function that processes changed documents.
Azure
public static void Run([CosmosDBTrigger(databaseName: [1], collectionName: [2], ConnectionStringSetting: [3], LeaseCollectionName = "leases")] IReadOnlyList<MyDocument> input, ILogger log) { foreach (var doc in input) { log.LogInformation($"Document id: {doc.id} processed."); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the lease collection name as the connection string setting.
Mixing up database and collection names.
✗ Incorrect
The database name is 'MyDb', collection name is 'MyCollection', and connection string setting is 'CosmosDBConnection'.