Skip to content

Commit bc2cd94

Browse files
authored
Merge pull request #114589 from timsander1/master
add doc for working with JSON
2 parents 31bc6c0 + a804c5c commit bc2cd94

File tree

5 files changed

+301
-186
lines changed

5 files changed

+301
-186
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,15 @@
166166
- name: OFFSET LIMIT
167167
displayName: offset limit
168168
href: sql-query-offset-limit.md
169+
- name: Working with JSON
170+
displayName: alias, JSON, projection
171+
href: sql-query-working-with-json.md
169172
- name: Subquery
170173
displayName: subquery, exists
171174
href: sql-query-subquery.md
172175
- name: Joins
173176
displayName: join
174177
href: sql-query-join.md
175-
- name: Aliasing
176-
displayName: alias, aliasing
177-
href: sql-query-aliasing.md
178178
- name: Arrays and objects
179179
displayName: object and array creation, object and array
180180
href: sql-query-object-array.md

articles/cosmos-db/sql-query-aliasing.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

articles/cosmos-db/sql-query-from.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ description: Learn about the SQL syntax, and example for FROM clause for Azure C
44
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 12/02/2019
7+
ms.date: 05/08/2020
88
ms.author: tisande
99

1010
---
1111
# FROM clause in Azure Cosmos DB
1212

1313
The FROM (`FROM <from_specification>`) clause is optional, unless the source is filtered or projected later in the query. A query like `SELECT * FROM Families` enumerates over the entire `Families` container. You can also use the special identifier ROOT for the container instead of using the container name.
1414

15-
The FROM clause enforces the following rules per query:
15+
The `FROM` clause enforces the following rules per query:
1616

17-
* The container can be aliased, such as `SELECT f.id FROM Families AS f` or simply `SELECT f.id FROM Families f`. Here `f` is the alias for `Families`. AS is an optional keyword to [alias](sql-query-aliasing.md) the identifier.
17+
* The container can be aliased, such as `SELECT f.id FROM Families AS f` or simply `SELECT f.id FROM Families f`. Here `f` is the alias for `Families`. AS is an optional keyword to [alias](sql-query-working-with-json.md#aliasing) the identifier.
1818

1919
* Once aliased, the original source name cannot be bound. For example, `SELECT Families.id FROM Families f` is syntactically invalid because the identifier `Families` has been aliased and can't be resolved anymore.
2020

@@ -25,15 +25,15 @@ The FROM clause enforces the following rules per query:
2525
```sql
2626
FROM <from_specification>
2727

28-
<from_specification> ::=
28+
<from_specification> ::=
2929
<from_source> {[ JOIN <from_source>][,...n]}
3030

31-
<from_source> ::=
31+
<from_source> ::=
3232
<container_expression> [[AS] input_alias]
3333
| input_alias IN <container_expression>
3434

35-
<container_expression> ::=
36-
ROOT
35+
<container_expression> ::=
36+
ROOT
3737
| container_name
3838
| input_alias
3939
| <container_expression> '.' property_name
@@ -46,9 +46,9 @@ FROM <from_specification>
4646

4747
Specifies a data source, with or without an alias. If alias is not specified, it will be inferred from the `<container_expression>` using following rules:
4848

49-
- If the expression is a container_name, then container_name will be used as an alias.
49+
- If the expression is a container_name, then container_name will be used as an alias.
5050

51-
- If the expression is `<container_expression>`, then property_name, then property_name will be used as an alias. If the expression is a container_name, then container_name will be used as an alias.
51+
- If the expression is `<container_expression>`, then property_name, then property_name will be used as an alias. If the expression is a container_name, then container_name will be used as an alias.
5252

5353
- AS `input_alias`
5454

@@ -94,9 +94,9 @@ If a container expression accesses properties or array elements and that value d
9494

9595
A container expression may be container-scoped or document-scoped:
9696

97-
- An expression is container-scoped, if the underlying source of the container expression is either ROOT or `container_name`. Such an expression represents a set of documents retrieved from the container directly, and is not dependent on the processing of other container expressions.
97+
- An expression is container-scoped, if the underlying source of the container expression is either ROOT or `container_name`. Such an expression represents a set of documents retrieved from the container directly, and is not dependent on the processing of other container expressions.
9898

99-
- An expression is document-scoped, if the underlying source of the container expression is `input_alias` introduced earlier in the query. Such an expression represents a set of documents obtained by evaluating the container expression in the scope of each document belonging to the set associated with the aliased container. The resulting set will be a union of sets obtained by evaluating the container expression for each of the documents in the underlying set.
99+
- An expression is document-scoped, if the underlying source of the container expression is `input_alias` introduced earlier in the query. Such an expression represents a set of documents obtained by evaluating the container expression in the scope of each document belonging to the set associated with the aliased container. The resulting set will be a union of sets obtained by evaluating the container expression for each of the documents in the underlying set.
100100

101101
## Examples
102102

@@ -142,7 +142,7 @@ The results are:
142142
]
143143
```
144144

145-
The preceding query used an array as the source, but you can also use an object as the source. The query considers any valid, defined JSON value in the source for inclusion in the result. The following example would exclude `Families` that dont have an `address.state` value.
145+
The preceding query used an array as the source, but you can also use an object as the source. The query considers any valid, defined JSON value in the source for inclusion in the result. The following example would exclude `Families` that don't have an `address.state` value.
146146

147147
```sql
148148
SELECT *

articles/cosmos-db/sql-query-select.md

Lines changed: 9 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
---
22
title: SELECT clause in Azure Cosmos DB
33
description: Learn about SQL SELECT clause for Azure Cosmos DB. Use SQL as an Azure Cosmos DB JSON query language.
4-
author: ginarobinson
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 06/10/2019
8-
ms.author: girobins
7+
ms.date: 05/08/2020
8+
ms.author: tisande
99
---
1010

1111
# SELECT clause in Azure Cosmos DB
1212

13-
Every query consists of a SELECT clause and optional [FROM](sql-query-from.md) and [WHERE](sql-query-where.md) clauses, per ANSI SQL standards. Typically, the source in the FROM clause is enumerated, and the WHERE clause applies a filter on the source to retrieve a subset of JSON items. The SELECT clause then projects the requested JSON values in the select list.
13+
Every query consists of a `SELECT` clause and optional [FROM](sql-query-from.md) and [WHERE](sql-query-where.md) clauses, per ANSI SQL standards. Typically, the source in the `FROM` clause is enumerated, and the `WHERE` clause applies a filter on the source to retrieve a subset of JSON items. The `SELECT` clause then projects the requested JSON values in the select list.
1414

1515
## Syntax
1616

1717
```sql
1818
SELECT <select_specification>
1919

20-
<select_specification> ::=
21-
'*'
22-
| [DISTINCT] <object_property_list>
20+
<select_specification> ::=
21+
'*'
22+
| [DISTINCT] <object_property_list>
2323
| [DISTINCT] VALUE <scalar_expression> [[ AS ] value_alias]
2424

25-
<object_property_list> ::=
25+
<object_property_list> ::=
2626
{ <scalar_expression> [ [ AS ] property_alias ] } [ ,...n ]
27-
2827
```
2928

3029
## Arguments
@@ -44,7 +43,7 @@ SELECT <select_specification>
4443
- `VALUE`
4544

4645
Specifies that the JSON value should be retrieved instead of the complete JSON object. This, unlike `<property_list>` does not wrap the projected value in an object.
47-
46+
4847
- `DISTINCT`
4948

5049
Specifies that duplicates of projected properties should be removed.
@@ -93,122 +92,6 @@ The results are:
9392
}]
9493
```
9594

96-
### Quoted property accessor
97-
You can access properties using the quoted property operator []. For example, `SELECT c.grade` and `SELECT c["grade"]` are equivalent. This syntax is useful to escape a property that contains spaces, special characters, or has the same name as a SQL keyword or reserved word.
98-
99-
```sql
100-
SELECT f["lastName"]
101-
FROM Families f
102-
WHERE f["id"] = "AndersenFamily"
103-
```
104-
105-
### Nested properties
106-
107-
The following example projects two nested properties, `f.address.state` and `f.address.city`.
108-
109-
```sql
110-
SELECT f.address.state, f.address.city
111-
FROM Families f
112-
WHERE f.id = "AndersenFamily"
113-
```
114-
115-
The results are:
116-
117-
```json
118-
[{
119-
"state": "WA",
120-
"city": "Seattle"
121-
}]
122-
```
123-
### JSON expressions
124-
125-
Projection also supports JSON expressions, as shown in the following example:
126-
127-
```sql
128-
SELECT { "state": f.address.state, "city": f.address.city, "name": f.id }
129-
FROM Families f
130-
WHERE f.id = "AndersenFamily"
131-
```
132-
133-
The results are:
134-
135-
```json
136-
[{
137-
"$1": {
138-
"state": "WA",
139-
"city": "Seattle",
140-
"name": "AndersenFamily"
141-
}
142-
}]
143-
```
144-
145-
In the preceding example, the SELECT clause needs to create a JSON object, and since the sample provides no key, the clause uses the implicit argument variable name `$1`. The following query returns two implicit argument variables: `$1` and `$2`.
146-
147-
```sql
148-
SELECT { "state": f.address.state, "city": f.address.city },
149-
{ "name": f.id }
150-
FROM Families f
151-
WHERE f.id = "AndersenFamily"
152-
```
153-
154-
The results are:
155-
156-
```json
157-
[{
158-
"$1": {
159-
"state": "WA",
160-
"city": "Seattle"
161-
},
162-
"$2": {
163-
"name": "AndersenFamily"
164-
}
165-
}]
166-
```
167-
## Reserved keywords and special characters
168-
169-
If your data contains properties with the same names as reserved keywords such as "order" or "Group" then the queries against these documents will result in syntax errors. You should explicitly include the property in `[]` character to run the query successfully.
170-
171-
For example, here's a document with a property named `order` and a property `price($)` that contains special characters:
172-
173-
```json
174-
{
175-
"id": "AndersenFamily",
176-
"order": [
177-
{
178-
"orderId": "12345",
179-
"productId": "A17849",
180-
"price($)": 59.33
181-
}
182-
],
183-
"creationDate": 1431620472,
184-
"isRegistered": true
185-
}
186-
```
187-
188-
If you run a queries that includes the `order` property or `price($)` property, you will receive a syntax error.
189-
190-
```sql
191-
SELECT * FROM c where c.order.orderid = "12345"
192-
```
193-
```sql
194-
SELECT * FROM c where c.order.price($) > 50
195-
```
196-
The result is:
197-
198-
`
199-
Syntax error, incorrect syntax near 'order'
200-
`
201-
202-
You should rewrite the same queries as below:
203-
204-
```sql
205-
SELECT * FROM c WHERE c["order"].orderId = "12345"
206-
```
207-
208-
```sql
209-
SELECT * FROM c WHERE c["order"]["price($)"] > 50
210-
```
211-
21295
## Next steps
21396

21497
- [Getting started](sql-query-getting-started.md)

0 commit comments

Comments
 (0)