Skip to content

Commit ca05ad5

Browse files
authored
Merge pull request #104170 from timsander1/master
add clarity for ORDER BY behavior for undefined paths
2 parents 4dedace + 1f1759c commit ca05ad5

File tree

1 file changed

+112
-8
lines changed

1 file changed

+112
-8
lines changed

articles/cosmos-db/sql-query-order-by.md

Lines changed: 112 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: ORDER BY clause in Azure Cosmos DB
33
description: Learn about SQL ORDER BY clause for Azure Cosmos DB. Use SQL as an Azure Cosmos DB JSON query language.
4-
author: markjbrown
4+
author: timsander1
55
ms.service: cosmos-db
66
ms.topic: conceptual
7-
ms.date: 06/10/2019
8-
ms.author: mjbrown
7+
ms.date: 02/12/2020
8+
ms.author: tisande
99

1010
---
1111
# ORDER BY clause in Azure Cosmos DB
@@ -44,10 +44,10 @@ ORDER BY <sort_specification>
4444

4545
## Remarks
4646

47-
The ORDER BY clause requires that the indexing policy include an index for the fields being sorted. The Azure Cosmos DB query runtime supports sorting against a property name and not against computed properties. Azure Cosmos DB supports multiple ORDER BY properties. In order to run a query with multiple ORDER BY properties, you should define a [composite index](index-policy.md#composite-indexes) on the fields being sorted.
48-
49-
> [!Note]
50-
> If the properties being sorted against might be undefined for some documents and you want to retrieve them in an ORDER BY query, you must explicitly create an index on those properties. The default indexing policy won't allow for the retrieval of the documents where the sort property is undefined.
47+
The `ORDER BY` clause requires that the indexing policy include an index for the fields being sorted. The Azure Cosmos DB query runtime supports sorting against a property name and not against computed properties. Azure Cosmos DB supports multiple `ORDER BY` properties. In order to run a query with multiple ORDER BY properties, you should define a [composite index](index-policy.md#composite-indexes) on the fields being sorted.
48+
49+
> [!Note]
50+
> If the properties being sorted might be undefined for some documents and you want to retrieve them in an ORDER BY query, you must explicitly include this path in the index. The default indexing policy won't allow for the retrieval of the documents where the sort property is undefined. [Review example queries on documents with some missing fields](#documents-with-missing-fields).
5151
5252
## Examples
5353

@@ -107,8 +107,112 @@ Additionally, you can order by multiple properties. A query that orders by multi
107107

108108
This query retrieves the family `id` in ascending order of the city name. If multiple items have the same city name, the query will order by the `creationDate` in descending order.
109109

110+
## Documents with missing fields
111+
112+
Queries with `ORDER BY` that are run against containers with the default indexing policy will not return documents where the sort property is undefined. If you would like to include documents where the sort property is undefined, you should explicitly include this property in the indexing policy.
113+
114+
For example, here's a container with an indexing policy that does not explicitly include any paths besides `"/*"`:
115+
116+
```json
117+
{
118+
"indexingMode": "consistent",
119+
"automatic": true,
120+
"includedPaths": [
121+
{
122+
"path": "/*"
123+
}
124+
],
125+
"excludedPaths": []
126+
}
127+
```
128+
129+
If you run a query that includes `lastName` in the `Order By` clause, the results will only include documents that have a `lastName` property defined. We have not defined an explicit included path for `lastName` so any documents without a `lastName` will not appear in the query results.
130+
131+
Here is a query that sorts by `lastName` on two documents, one of which does not have a `lastName` defined:
132+
133+
```sql
134+
SELECT f.id, f.lastName
135+
FROM Families f
136+
ORDER BY f.lastName
137+
```
138+
139+
The results only include the document that has a defined `lastName`:
140+
141+
```json
142+
[
143+
{
144+
"id": "AndersenFamily",
145+
"lastName": "Andersen"
146+
}
147+
]
148+
```
149+
150+
If we update the container's indexing policy to explicitly include a path for `lastName`, we will include documents with an undefined sort property in the query results. You must explicitly define the path to lead to this scalar value (and not beyond it). You should use the `?` character in your path definition in the indexing policy to ensure that you explicitly index the property `lastName` and no additional nested paths beyond it.
151+
152+
Here is a sample indexing policy which allows you to have documents with an undefined `lastName` appear in the query results:
153+
154+
```json
155+
{
156+
"indexingMode": "consistent",
157+
"automatic": true,
158+
"includedPaths": [
159+
{
160+
"path": "/lastName/?"
161+
},
162+
{
163+
"path": "/*"
164+
}
165+
],
166+
"excludedPaths": []
167+
}
168+
```
169+
170+
If you run the same query again, documents that are missing `lastName` appear first in the query results:
171+
172+
```sql
173+
SELECT f.id, f.lastName
174+
FROM Families f
175+
ORDER BY f.lastName
176+
```
177+
178+
The results are:
179+
180+
```json
181+
[
182+
{
183+
"id": "WakefieldFamily"
184+
},
185+
{
186+
"id": "AndersenFamily",
187+
"lastName": "Andersen"
188+
}
189+
]
190+
```
191+
192+
If you modify the sort order to `DESC`, documents that are missing `lastName` appear last in the query results:
193+
194+
```sql
195+
SELECT f.id, f.lastName
196+
FROM Families f
197+
ORDER BY f.lastName DESC
198+
```
199+
200+
The results are:
201+
202+
```json
203+
[
204+
{
205+
"id": "AndersenFamily",
206+
"lastName": "Andersen"
207+
},
208+
{
209+
"id": "WakefieldFamily"
210+
}
211+
]
212+
```
213+
110214
## Next steps
111215

112216
- [Getting started](sql-query-getting-started.md)
113-
- [SELECT clause](sql-query-select.md)
217+
- [Indexing policies in Azure Cosmos DB](index-policy.md)
114218
- [OFFSET LIMIT clause](sql-query-offset-limit.md)

0 commit comments

Comments
 (0)