Cosmos DB allows us to save data in arrays and provides in-built functions to interact with array data.
Here is an example of a simple document that has an array field with key “tags”.
{
“id”: “1”,
“name”: “John Doe”,
“tags”: [“tag1”, “tag2”, “tag3”, “tag4”]
}
Let’s say we want to find a document where “tags” array contains the value “tag1”. Here is how we can build the query.
SELECT * FROM c WHERE ARRAY_CONTAINS(c.tags, “tag1”)
2. WORKING WITH NESTED OBJECTS
Cosmos DB supports storing nested objects and offers querying capabilities on them.
If we have a document like the following:
{
“id”: “1”,
“name”: “John Doe”,
“address”: {
“city”: “Los Angeles”,
“state”: “California”
}
}
We could find John Doe by searching for him based on his city or state using nested queries.
SELECT * FROM c WHERE c.address.city = “Los Angeles”
3. USING AGGREGATE FUNCTIONS
Cosmos DB supports numerous aggregate functions like COUNT, SUM, MIN, MAX, and AVG. These functions could be very useful in performing powerful computations over large sets of data.
If we have documents describing products with fields “price” and “category”, we can calculate the average price of all products in a certain category using the AVG function.
SELECT VALUE AVG(c.price) FROM c WHERE c.category = “electronics”
4. ORDERING DATA
Cosmos DB supports ordering of data during query execution through the use of the ORDER BY clause.
Given the product documents from the previous example, we can retrieve all electronics products sorted by their price in descending order as follows:
SELECT * FROM c WHERE c.category = “electronics” ORDER BY c.price DESC
However, to perform an ORDER BY query, an index on the ordered property must be declared in the collection definition when it’s created.
For an effective application of these querying techniques, it is crucial to design the Cosmos DB container, partition, and items properly while considering the querying requirements. Furthermore, emphasis should be on understanding the data access patterns and laying out the data in such a way that it suits the Cosmos DB model, which ensures consistency, scalability, and speed.
In preparation for the DP-420 Designing and Implementing Native Applications Using Microsoft Azure Cosmos DB exam, it is vital to have a firm grasp on these functionalities, as they form the backbone of interacting with data in Cosmos DB.
Practice Test
True or False: In Azure Cosmos DB, it’s possible to use the SQL API to query arrays.
• True
• False
Answer: True
Explanation: Azure Cosmos DB’s SQL API supports querying of arrays. Arrays can be queried in various ways, including the index of the array, nested arrays, and multi-level nesting.
In Azure CosmosDB, is it possible to use queries with nested objects?
• A. Yes
• B. No
Answer: A. Yes
Explanation: Azure Cosmos DB allows queries on nested objects. This includes applying filters on properties of nested objects, joining on properties of nested objects, etc.
Does Azure Cosmos DB support the usage of the SQL SUM function in aggregates?
• A. Yes
• B. No
Answer: B. No
Explanation: While Azure Cosmos DB does support some SQL aggregate functions such as COUNT and AVG, the SUM function is not supported.
When implementing ordering in Cosmos DB, what should you keep in mind?
• A. Partition key
• B. Filter predicate
• C. Index
• D. All of the above
Answer: D. All of the above
Explanation: To optimize performance in Cosmos DB, you should consider the partition key for data distribution, filter predicates to limit the amount of data scanned, and indexing for sorting and filtering.
True or False: The ORDER BY clause in Azure Cosmos DB is case sensitive.
• True
• False
Answer: False
Explanation: The ORDER BY clause in Azure Cosmos DB is not case sensitive.
True or False: Aggregation queries can span multiple partition keys in Azure Cosmos DB.
• True
• False
Answer: True
Explanation: Aggregation queries can span multiple partition keys in Azure Cosmos DB, but they consume more RU/s and process slower.
What do we use to make modifications to a document in Cosmos DB?
• A. SQL Query
• B. Stored Procedure
• C. Both
Answer: B. Stored Procedure
Explanation: While queries in Cosmos DB can be used to read a document, modifications (like updates or deletions) are often performed using stored procedures.
True or False: You can use GROUP BY with multiple properties within an aggregate query.
• True
• False
Answer: True
Explanation: Azure Cosmos DB allows you to use GROUP BY with multiple properties within an aggregate query to group the results by the specified properties.
Which of the following aggregation functions are supported by Azure Cosmos DB?
• A. COUNT
• B. SUM
• C. MIN
• D. MAX
• E. All of the above
Answer: E. All of the above
Explanation: Cosmos DB supports all of the mentioned aggregation functions in the SQL API.
Is it possible to sort array values within a document in Azure Cosmos DB by default?
• A. Yes
• B. No
Answer: B. No
Explanation: By default, Azure Cosmos DB doesn’t allow you to sort array values within a document. However, you can use custom ordering logic in client-side code to do so.
Interview Questions
1. How can you perform queries that involve arrays in Microsoft Azure Cosmos DB?
To perform queries involving arrays in Cosmos DB, you can use the ARRAY_CONTAINS function.
2. In Cosmos DB, how do you query nested objects within a document?
To query nested objects within a document in Cosmos DB, you can use dot notation to access the nested fields.
3. What is aggregation in Microsoft Azure Cosmos DB, and how is it used in queries?
Aggregation in Cosmos DB involves grouping and performing operations on multiple documents. It is used for operations like counting, summing, averaging, etc.
4. How can you sort query results in ascending order in Cosmos DB?
To sort query results in ascending order, you can use the ORDER BY clause in your Cosmos DB query.
5. Are there any limitations when working with nested objects in queries within Azure Cosmos DB?
Yes, as of the current version, Cosmos DB has limitations on the depth of the nested objects that can be queried.
6. How does the JOIN operation work in Cosmos DB for querying arrays?
Cosmos DB does not support the JOIN operation. Instead, you can denormalize the data to work with arrays efficiently.
7. Can you use SQL-like syntax to query arrays and nested objects in Cosmos DB?
Yes, Cosmos DB supports SQL-like syntax for querying arrays and nested objects within documents.
8. What is the significance of the Root() function in Cosmos DB queries?
The Root() function in Cosmos DB queries is used to refer to the root document being queried.
9. How can you filter query results based on nested object properties in Cosmos DB?
You can filter query results based on nested object properties by specifying the criteria within the query using dot notation.
10. Is it possible to perform complex queries that involve both arrays and nested objects simultaneously in Cosmos DB?
Yes, you can construct complex queries in Cosmos DB that involve both arrays and nested objects to retrieve specific data based on your requirements.