Skip to content

Commit cfb1eee

Browse files
authored
Merge pull request #246893 from seesharprun/cosmos-nosql-query-rework
Cosmos DB | Add NoSQL Query reworked articles
2 parents a72264e + 189a17f commit cfb1eee

File tree

4 files changed

+358
-369
lines changed

4 files changed

+358
-369
lines changed
Lines changed: 71 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,207 +1,147 @@
11
---
2-
title: Working with arrays and objects in Azure Cosmos DB
3-
description: Learn the SQL syntax to create arrays and objects in Azure Cosmos DB. This article also provides some examples to perform operations on array objects
4-
author: seesharprun
2+
title: Working with arrays and objects
3+
titleSuffix: Azure Cosmos DB for NoSQL
4+
description: Create arrays and objects and perform actions on them using the array syntax in Azure Cosmos DB for NoSQL.
5+
author: jcodella
6+
ms.author: jacodel
7+
ms.reviewer: sidandrews
58
ms.service: cosmos-db
69
ms.subservice: nosql
7-
ms.custom: ignite-2022
8-
ms.topic: conceptual
9-
ms.date: 02/02/2021
10-
ms.author: sidandrews
11-
ms.reviewer: jucocchi
10+
ms.topic: reference
11+
ms.date: 07/31/2023
12+
ms.custom: query-reference
1213
---
13-
# Working with arrays and objects in Azure Cosmos DB
14-
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1514

16-
A key feature of the Azure Cosmos DB for NoSQL is array and object creation. This document uses examples that can be recreated using the [Family dataset](getting-started.md#upload-sample-data).
15+
# Working with arrays and objects in Azure Cosmos DB for NoSQL
16+
17+
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1718

18-
Here's an example item in this dataset:
19+
Here's an item that's used in examples throughout this article.
1920

2021
```json
2122
{
22-
"id": "AndersenFamily",
23-
"lastName": "Andersen",
24-
"parents": [
25-
{ "firstName": "Thomas" },
26-
{ "firstName": "Mary Kay"}
23+
"name": "Sondon Fins",
24+
"categories": [
25+
{ "name": "swim" },
26+
{ "name": "gear"}
2727
],
28-
"children": [
29-
{
30-
"firstName": "Henriette Thaulow",
31-
"gender": "female",
32-
"grade": 5,
33-
"pets": [{ "givenName": "Fluffy" }]
34-
}
35-
],
36-
"address": { "state": "WA", "county": "King", "city": "Seattle" },
37-
"creationDate": 1431620472,
38-
"isRegistered": true
28+
"metadata": {
29+
"sku": "73310",
30+
"manufacturer": "AdventureWorks"
31+
},
32+
"priceInUSD": 132.35,
33+
"priceInCAD": 174.50
3934
}
4035
```
4136

4237
## Arrays
4338

44-
You can construct arrays, as shown in the following example:
39+
You can construct arrays using static values, as shown in the following example.
4540

4641
```sql
47-
SELECT [f.address.city, f.address.state] AS CityState
48-
FROM Families f
42+
SELECT
43+
[p.priceInUSD, p.priceInCAD] AS priceData
44+
FROM products p
4945
```
5046

51-
The results are:
52-
5347
```json
5448
[
5549
{
56-
"CityState": [
57-
"Seattle",
58-
"WA"
59-
]
60-
},
61-
{
62-
"CityState": [
63-
"NY",
64-
"NY"
50+
"priceData": [
51+
132.35,
52+
174.5
6553
]
6654
}
6755
]
6856
```
6957

70-
You can also use the [ARRAY expression](subquery.md#array-expression) to construct an array from [subquery's](subquery.md) results. This query gets all the distinct given names of children in an array.
58+
You can also use the [``ARRAY`` expression](subquery.md#array-expression) to construct an array from a [subquery's](subquery.md) results. This query gets all the distinct categories.
7159

7260
```sql
73-
SELECT f.id, ARRAY(SELECT DISTINCT VALUE c.givenName FROM c IN f.children) as ChildNames
74-
FROM f
61+
SELECT
62+
p.id,
63+
ARRAY (SELECT DISTINCT VALUE c.name FROM c IN p.categories) AS categoryNames
64+
FROM
65+
products p
7566
```
7667

77-
The results are:
78-
7968
```json
8069
[
81-
{
82-
"id": "AndersenFamily",
83-
"ChildNames": []
84-
},
85-
{
86-
"id": "WakefieldFamily",
87-
"ChildNames": [
88-
"Jesse",
89-
"Lisa"
90-
]
91-
}
70+
{
71+
"id": "a0151c77-ffc3-4fa6-a495-7b53d936faa6",
72+
"categoryNames": [
73+
"swim",
74+
"gear"
75+
]
76+
}
9277
]
9378
```
9479

95-
## <a id="Iteration"></a>Iteration
96-
97-
The API for NoSQL provides support for iterating over JSON arrays, with the [IN keyword](keywords.md#in) in the FROM source. In the following example:
98-
99-
```sql
100-
SELECT *
101-
FROM Families.children
102-
```
103-
104-
The results are:
80+
## Iteration
10581

106-
```json
107-
[
108-
[
109-
{
110-
"firstName": "Henriette Thaulow",
111-
"gender": "female",
112-
"grade": 5,
113-
"pets": [{ "givenName": "Fluffy"}]
114-
}
115-
],
116-
[
117-
{
118-
"familyName": "Merriam",
119-
"givenName": "Jesse",
120-
"gender": "female",
121-
"grade": 1
122-
},
123-
{
124-
"familyName": "Miller",
125-
"givenName": "Lisa",
126-
"gender": "female",
127-
"grade": 8
128-
}
129-
]
130-
]
131-
```
82+
The API for NoSQL provides support for iterating over JSON arrays, with the [``IN`` keyword](keywords.md#in) in the ``FROM`` source.
13283

133-
The next query performs iteration over `children` in the `Families` container. The output array is different from the preceding query. This example splits `children`, and flattens the results into a single array:
84+
As an example, the next query performs iteration over ``tags`` for each item in the container. The output splits the array value and flattens the results into a single array.
13485

13586
```sql
136-
SELECT *
137-
FROM c IN Families.children
87+
SELECT
88+
*
89+
FROM
90+
products IN products.categories
13891
```
13992

140-
The results are:
141-
14293
```json
14394
[
14495
{
145-
"firstName": "Henriette Thaulow",
146-
"gender": "female",
147-
"grade": 5,
148-
"pets": [{ "givenName": "Fluffy" }]
149-
},
150-
{
151-
"familyName": "Merriam",
152-
"givenName": "Jesse",
153-
"gender": "female",
154-
"grade": 1
96+
"name": "swim"
15597
},
15698
{
157-
"familyName": "Miller",
158-
"givenName": "Lisa",
159-
"gender": "female",
160-
"grade": 8
99+
"name": "gear"
161100
}
162101
]
163102
```
164103

165104
You can filter further on each individual entry of the array, as shown in the following example:
166105

167106
```sql
168-
SELECT c.givenName
169-
FROM c IN Families.children
170-
WHERE c.grade = 8
107+
SELECT VALUE
108+
p.name
109+
FROM
110+
p IN p.categories
111+
WHERE
112+
p.name LIKE "ge%"
171113
```
172114

173115
The results are:
174116

175117
```json
176-
[{
177-
"givenName": "Lisa"
178-
}]
118+
[
119+
"gear"
120+
]
179121
```
180122

181-
You can also aggregate over the result of an array iteration. For example, the following query counts the number of children among all families:
123+
You can also aggregate over the result of an array iteration. For example, the following query counts the number of tags:
182124

183125
```sql
184-
SELECT COUNT(1) AS Count
185-
FROM child IN Families.children
126+
SELECT VALUE
127+
COUNT(1)
128+
FROM
129+
p IN p.categories
186130
```
187131

188132
The results are:
189133

190134
```json
191135
[
192-
{
193-
"Count": 3
194-
}
136+
2
195137
]
196138
```
197139

198140
> [!NOTE]
199-
> When using the IN keyword for iteration, you cannot filter or project any properties outside of the array. Instead, you should use [JOINs](join.md).
200-
201-
For additional examples, read our [blog post on working with arrays in Azure Cosmos DB](https://devblogs.microsoft.com/cosmosdb/understanding-how-to-query-arrays-in-azure-cosmos-db/).
141+
> When using the ``IN`` keyword for iteration, you cannot filter or project any properties outside of the array. Instead, you should use [self-joins](join.md).
202142
203143
## Next steps
204144

205-
- [Getting started](getting-started.md)
206-
- [Azure Cosmos DB .NET samples](https://github.com/Azure/azure-cosmos-dotnet-v3)
207-
- [Joins](join.md)
145+
- [Self-joins](join.md)
146+
- [Keywords](keywords.md)
147+
- [Subqueries](subquery.md)
Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
11
---
2-
title: Pagination in Azure Cosmos DB
3-
description: Learn about paging concepts and continuation tokens
4-
author: seesharprun
5-
ms.author: sidandrews
6-
ms.reviewer: jucocchi
2+
title: Pagination
3+
titleSuffix: Azure Cosmos DB for NoSQL
4+
description: Page through multiple sets of results and use continuation tokens to continue pagination operators.
5+
author: jcodella
6+
ms.author: jacodel
7+
ms.reviewer: sidandrews
78
ms.service: cosmos-db
89
ms.subservice: nosql
9-
ms.custom: ignite-2022
10-
ms.topic: conceptual
11-
ms.date: 09/15/2021
10+
ms.topic: reference
11+
ms.date: 07/31/2023
12+
ms.custom: query-reference
1213
---
1314

14-
# Pagination in Azure Cosmos DB
15+
# Pagination in Azure Cosmos DB for NoSQL
16+
1517
[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)]
1618

17-
In Azure Cosmos DB, queries may have multiple pages of results. This document explains criteria that Azure Cosmos DB's query engine uses to decide whether to split query results into multiple pages. You can optionally use continuation tokens to manage query results that span multiple pages.
19+
In Azure Cosmos DB for NoSQL, queries may have multiple pages of results. This document explains criteria that Azure Cosmos DB for NoSQL's query engine uses to decide whether to split query results into multiple pages. You can optionally use continuation tokens to manage query results that span multiple pages.
1820

1921
## Understanding query executions
2022

21-
Sometimes query results will be split over multiple pages. Each page's results is generated by a separate query execution. When query results cannot be returned in one single execution, Azure Cosmos DB will automatically split results into multiple pages.
23+
Sometimes query results are split over multiple pages. A separate query execution generates each page's results. When query results can't be returned in one single execution, Azure Cosmos DB for NoSQL automatically splits results into multiple pages.
2224

23-
You can specify the maximum number of items returned by a query by setting the `MaxItemCount`. The `MaxItemCount` is specified per request and tells the query engine to return that number of items or fewer. You can set `MaxItemCount` to `-1` if you don't want to place a limit on the number of results per query execution.
25+
You can specify the maximum number of items returned by a query by setting the ``MaxItemCount``. The ``MaxItemCount`` is specified per request and tells the query engine to return that number of items or fewer. You can set ``MaxItemCount`` to ``-1`` if you don't want to place a limit on the number of results per query execution.
2426

25-
In addition, there are other reasons that the query engine might need to split query results into multiple pages. These include:
27+
In addition, there are other reasons that the query engine might need to split query results into multiple pages. These reasons include:
2628

2729
- The container was throttled and there weren't available RUs to return more query results
2830
- The query execution's response was too large
2931
- The query execution's time was too long
30-
- It was more efficient for the query engine to return results in additional executions
32+
- It was more efficient for the query engine to return results in extra executions
3133

32-
The number of items returned per query execution will always be less than or equal to `MaxItemCount`. However, it is possible that other criteria might have limited the number of results the query could return. If you execute the same query multiple times, the number of pages might not be constant. For example, if a query is throttled there may be fewer available results per page, which means the query will have additional pages. In some cases, it is also possible that your query may return an empty page of results.
34+
The number of items returned per query execution are less than or equal to ``MaxItemCount``. However, it's possible that other criteria might have limited the number of results the query could return. If you execute the same query multiple times, the number of pages might not be constant. For example, if a query is throttled there may be fewer available results per page, which means the query has extra pages. In some cases, it's also possible that your query may return an empty page of results.
3335

3436
## Handling multiple pages of results
3537

36-
To ensure accurate query results, you should progress through all pages. You should continue to execute queries until there are no additional pages.
38+
To ensure accurate query results, you should progress through all pages. You should continue to execute queries until there are no extra pages.
3739

3840
Here are some examples for processing results from queries with multiple pages:
3941

@@ -44,7 +46,7 @@ Here are some examples for processing results from queries with multiple pages:
4446

4547
## Continuation tokens
4648

47-
In the .NET SDK and Java SDK you can optionally use continuation tokens as a bookmark for your query's progress. Azure Cosmos DB query executions are stateless at the server side and can be resumed at any time using the continuation token. For the Python SDK, continuation tokens are only supported for single partition queries. The partition key must be specified in the options object because it's not sufficient to have it in the query itself.
49+
In the .NET SDK and Java SDK, you can optionally use continuation tokens as a bookmark for your query's progress. Azure Cosmos DB for NoSQL query executions are stateless at the server side and can be resumed at any time using the continuation token. For the Python SDK, continuation tokens are only supported for single partition queries. The partition key must be specified in the options object because it's not sufficient to have it in the query itself.
4850

4951
Here are some example for using continuation tokens:
5052

@@ -53,24 +55,26 @@ Here are some example for using continuation tokens:
5355
- [Node.js SDK](https://github.com/Azure/azure-sdk-for-js/blob/2186357a6e6a64b59915d0cf3cba845be4d115c4/sdk/cosmosdb/cosmos/samples/src/BulkUpdateWithSproc.ts#L16-L31)
5456
- [Python SDK](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/cosmos/azure-cosmos/test/test_query.py#L533)
5557

56-
If the query returns a continuation token, then there are additional query results.
58+
If the query returns a continuation token, then there are extra query results.
5759

58-
In Azure Cosmos DB's REST API, you can manage continuation tokens with the `x-ms-continuation` header. As with querying with the .NET or Java SDK, if the `x-ms-continuation` response header is not empty, it means the query has additional results.
60+
In Azure Cosmos DB for NoSQL's REST API, you can manage continuation tokens with the ``x-ms-continuation`` header. As with querying with the .NET or Java SDK, if the ``x-ms-continuation`` response header isn't empty, it means the query has extra results.
5961

60-
As long as you are using the same SDK version, continuation tokens never expire. You can optionally [restrict the size of a continuation token](/dotnet/api/microsoft.azure.documents.client.feedoptions.responsecontinuationtokenlimitinkb). Regardless of the amount of data or number of physical partitions in your container, queries return a single continuation token.
62+
As long as you're using the same SDK version, continuation tokens never expire. You can optionally [restrict the size of a continuation token](/dotnet/api/microsoft.azure.documents.client.feedoptions.responsecontinuationtokenlimitinkb). Regardless of the amount of data or number of physical partitions in your container, queries return a single continuation token.
6163

62-
You cannot use continuation tokens for queries with [GROUP BY](group-by.md) or [DISTINCT](keywords.md#distinct) because these queries would require storing a significant amount of state. For queries with `DISTINCT`, you can use continuation tokens if you add `ORDER BY` to the query.
64+
You can't use continuation tokens for queries with [GROUP BY](group-by.md) or [DISTINCT](keywords.md#distinct) because these queries would require storing a significant amount of state. For queries with ``DISTINCT``, you can use continuation tokens if you add ``ORDER BY`` to the query.
6365

64-
Here's an example of a query with `DISTINCT` that could use a continuation token:
66+
Here's an example of a query with ``DISTINCT`` that could use a continuation token:
6567

6668
```sql
67-
SELECT DISTINCT VALUE c.name
68-
FROM c
69-
ORDER BY c.name
69+
SELECT DISTINCT VALUE
70+
e.name
71+
FROM
72+
employees e
73+
ORDER BY
74+
e.name
7075
```
7176

7277
## Next steps
7378

74-
- [Introduction to Azure Cosmos DB](../../introduction.md)
75-
- [Azure Cosmos DB .NET samples](https://github.com/Azure/azure-cosmos-dotnet-v3)
76-
- [ORDER BY clause](order-by.md)
79+
- [``ORDER BY`` clause](order-by.md)
80+
- [``OFFSET LIMIT`` clause](offset-limit.md)

0 commit comments

Comments
 (0)