|
1 | 1 | --- |
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 |
5 | 8 | ms.service: cosmos-db |
6 | 9 | 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 |
12 | 13 | --- |
13 | | -# Working with arrays and objects in Azure Cosmos DB |
14 | | -[!INCLUDE[NoSQL](../../includes/appliesto-nosql.md)] |
15 | 14 |
|
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)] |
17 | 18 |
|
18 | | -Here's an example item in this dataset: |
| 19 | +Here's an item that's used in examples throughout this article. |
19 | 20 |
|
20 | 21 | ```json |
21 | 22 | { |
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"} |
27 | 27 | ], |
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 |
39 | 34 | } |
40 | 35 | ``` |
41 | 36 |
|
42 | 37 | ## Arrays |
43 | 38 |
|
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. |
45 | 40 |
|
46 | 41 | ```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 |
49 | 45 | ``` |
50 | 46 |
|
51 | | -The results are: |
52 | | - |
53 | 47 | ```json |
54 | 48 | [ |
55 | 49 | { |
56 | | - "CityState": [ |
57 | | - "Seattle", |
58 | | - "WA" |
59 | | - ] |
60 | | - }, |
61 | | - { |
62 | | - "CityState": [ |
63 | | - "NY", |
64 | | - "NY" |
| 50 | + "priceData": [ |
| 51 | + 132.35, |
| 52 | + 174.5 |
65 | 53 | ] |
66 | 54 | } |
67 | 55 | ] |
68 | 56 | ``` |
69 | 57 |
|
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. |
71 | 59 |
|
72 | 60 | ```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 |
75 | 66 | ``` |
76 | 67 |
|
77 | | -The results are: |
78 | | - |
79 | 68 | ```json |
80 | 69 | [ |
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 | + } |
92 | 77 | ] |
93 | 78 | ``` |
94 | 79 |
|
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 |
105 | 81 |
|
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. |
132 | 83 |
|
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. |
134 | 85 |
|
135 | 86 | ```sql |
136 | | -SELECT * |
137 | | -FROM c IN Families.children |
| 87 | +SELECT |
| 88 | + * |
| 89 | +FROM |
| 90 | + products IN products.categories |
138 | 91 | ``` |
139 | 92 |
|
140 | | -The results are: |
141 | | - |
142 | 93 | ```json |
143 | 94 | [ |
144 | 95 | { |
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" |
155 | 97 | }, |
156 | 98 | { |
157 | | - "familyName": "Miller", |
158 | | - "givenName": "Lisa", |
159 | | - "gender": "female", |
160 | | - "grade": 8 |
| 99 | + "name": "gear" |
161 | 100 | } |
162 | 101 | ] |
163 | 102 | ``` |
164 | 103 |
|
165 | 104 | You can filter further on each individual entry of the array, as shown in the following example: |
166 | 105 |
|
167 | 106 | ```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%" |
171 | 113 | ``` |
172 | 114 |
|
173 | 115 | The results are: |
174 | 116 |
|
175 | 117 | ```json |
176 | | -[{ |
177 | | - "givenName": "Lisa" |
178 | | -}] |
| 118 | +[ |
| 119 | + "gear" |
| 120 | +] |
179 | 121 | ``` |
180 | 122 |
|
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: |
182 | 124 |
|
183 | 125 | ```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 |
186 | 130 | ``` |
187 | 131 |
|
188 | 132 | The results are: |
189 | 133 |
|
190 | 134 | ```json |
191 | 135 | [ |
192 | | - { |
193 | | - "Count": 3 |
194 | | - } |
| 136 | + 2 |
195 | 137 | ] |
196 | 138 | ``` |
197 | 139 |
|
198 | 140 | > [!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). |
202 | 142 |
|
203 | 143 | ## Next steps |
204 | 144 |
|
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) |
0 commit comments