0
0
Azurecloud~10 mins

Functions with Cosmos DB integration in Azure - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A"AzureWebJobsStorage"
B"CosmosDBConnection"
C"SqlConnection"
D"EventHubConnection"
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.
2fill in blank
medium

Complete 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'
AAddAsync
BAdd
CWriteAsync
DInsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using synchronous Add method which does not exist.
Using incorrect method names like WriteAsync or Insert.
3fill in blank
hard

Fix 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'
APartitionKey
BPartitionKeyPath
CPartitionKeyValue
DPartitionKeySetting
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect property names like PartitionKeyPath or PartitionKeyValue.
Omitting the partition key when required.
4fill in blank
hard

Fill 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'
A"SELECT * FROM c WHERE c.status = 'active'"
B"SELECT * FROM c"
Cscore
Dstatus
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.
5fill in blank
hard

Fill 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'
A"MyDb"
B"MyCollection"
C"CosmosDBConnection"
D"leases"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the lease collection name as the connection string setting.
Mixing up database and collection names.