Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Functions with Cosmos DB integration
📖 Scenario: You are building a simple Azure Function that interacts with Cosmos DB to store and retrieve user data. This is a common pattern in cloud applications where serverless functions handle data operations.
🎯 Goal: Create an Azure Function that connects to a Cosmos DB container, inserts a user document, and queries users by a specific property.
📋 What You'll Learn
Create a Cosmos DB client connection string variable
Define a function to insert a user document into Cosmos DB
Define a function to query users by their city
Configure the Azure Function to use the Cosmos DB client
💡 Why This Matters
🌍 Real World
Serverless functions often need to store and retrieve data from databases like Cosmos DB. This project shows how to connect and perform basic operations.
💼 Career
Understanding how to integrate Azure Functions with Cosmos DB is a key skill for cloud developers working on scalable, event-driven applications.
Progress0 / 4 steps
1
Create Cosmos DB connection string variable
Create a variable called cosmos_connection_string and set it to the exact string "AccountEndpoint=https://mycosmos.documents.azure.com:443/;AccountKey=ABC123==;".
Azure
Hint
Use a simple string assignment to create the connection string variable.
2
Create Cosmos DB client variable
Using the cosmos_connection_string variable, create a Cosmos DB client variable called client by calling CosmosClient(cosmos_connection_string). Assume CosmosClient is already imported.
Azure
Hint
Instantiate the CosmosClient using the connection string variable with from_connection_string method.
3
Define function to insert a user document
Define a function called insert_user that takes parameters database_name, container_name, and user_data. Inside the function, get the container from client using client.get_database_client(database_name).get_container_client(container_name). Then call container.create_item(user_data) to insert the document.
Azure
Hint
Define the function with the exact name and parameters. Use the client to get the container and call create_item.
4
Define function to query users by city
Define a function called query_users_by_city that takes parameters database_name, container_name, and city. Inside the function, get the container from client as before. Then create a SQL query string "SELECT * FROM c WHERE c.city = @city". Use container.query_items with the query and parameters [{"name": "@city", "value": city}], and set enable_cross_partition_query=True. Return the list of results.
Azure
Hint
Define the function with the exact name and parameters. Use a parameterized SQL query and return the results as a list.
Practice
(1/5)
1. What is the main benefit of using Azure Functions with Cosmos DB integration?
easy
A. Automatically run code when data changes in Cosmos DB
B. Manually trigger code only through HTTP requests
C. Store large files directly in Cosmos DB
D. Replace Cosmos DB with Azure Blob Storage
Solution
Step 1: Understand Azure Functions with Cosmos DB
Azure Functions can be triggered automatically by changes in Cosmos DB data.
Step 2: Identify the main benefit
This automatic trigger saves resources by running code only when needed, without manual calls.
Final Answer:
Automatically run code when data changes in Cosmos DB -> Option A
Quick Check:
Functions trigger on data changes = A [OK]
Hint: Functions run on data change events automatically [OK]
Common Mistakes:
Thinking functions run only on HTTP triggers
Confusing Cosmos DB with file storage
Assuming manual triggers are required
2. Which of the following is the correct binding direction for a Cosmos DB input binding in an Azure Function?
easy
A. direction: "out"
B. direction: "both"
C. direction: "trigger"
D. direction: "in"
Solution
Step 1: Recall binding directions
Input bindings receive data into the function, so their direction is "in".
Step 2: Match binding direction for Cosmos DB input
Cosmos DB input binding must have direction set to "in" to read data.
Final Answer:
direction: "in" -> Option D
Quick Check:
Input binding direction = in [OK]
Hint: Input bindings always use direction "in" [OK]
Common Mistakes:
Using "out" for input bindings
Confusing trigger with input binding
Using invalid directions like "both"
3. Given this Azure Function code snippet triggered by Cosmos DB changes, what will be logged if a new document with id "123" is added?
module.exports = async function (context, documents) {
if (!!documents && documents.length > 0) {
context.log(`Document id: ${documents[0].id}`);
}
};
medium
A. No output logged
B. Document id: undefined
C. Document id: 123
D. Error: documents is not defined
Solution
Step 1: Understand the trigger input
The function receives an array 'documents' with changed documents; the first document has id "123".
Step 2: Analyze the logging statement
The code logs the id of the first document, which is "123".
Final Answer:
Document id: 123 -> Option C
Quick Check:
documents[0].id = 123 logged [OK]
Hint: documents array holds changed items; access first with documents[0] [OK]
Common Mistakes:
Assuming documents is undefined
Logging without checking documents length
Confusing document id property
4. You wrote an Azure Function triggered by Cosmos DB changes, but it never runs when documents change. Which is the most likely cause?
medium
A. The function code has a syntax error
B. The function.json binding has incorrect connection string name
C. The Cosmos DB container is empty
D. The function app is stopped
Solution
Step 1: Check trigger configuration
If the connection string name in function.json is wrong, the function won't connect to Cosmos DB changes.
Step 2: Consider other causes
Syntax errors cause failures but usually show errors; empty container still triggers on inserts; stopped app won't run but question implies function exists.
Final Answer:
The function.json binding has incorrect connection string name -> Option B
Quick Check:
Wrong connection string stops trigger [OK]
Hint: Check connection string name in function.json first [OK]
Common Mistakes:
Ignoring binding configuration errors
Assuming empty container prevents triggers
Not verifying function app status
5. You want to create an Azure Function that writes a summary document to Cosmos DB whenever multiple documents are added. Which binding setup should you use?
hard
A. Use Cosmos DB trigger for input and Cosmos DB output binding for summary document
B. Use HTTP trigger and Cosmos DB input binding only
C. Use Cosmos DB input binding only, no trigger
D. Use Timer trigger and Cosmos DB output binding only
Solution
Step 1: Identify trigger for reacting to data changes
Cosmos DB trigger runs the function automatically when documents change.
Step 2: Use output binding to write summary
Output binding lets the function write a new summary document back to Cosmos DB.
Final Answer:
Use Cosmos DB trigger for input and Cosmos DB output binding for summary document -> Option A
Quick Check:
Trigger input + output binding for writing = C [OK]
Hint: Trigger on changes, output binding to write summary [OK]