Skip to content

Commit 0c34d8c

Browse files
authored
Merge pull request #196391 from timsander1/master
update doc for ORDER BY bug fix
2 parents 42e7393 + 0964f5a commit 0c34d8c

File tree

1 file changed

+4
-96
lines changed

1 file changed

+4
-96
lines changed

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

Lines changed: 4 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: timsander1
55
ms.service: cosmos-db
66
ms.subservice: cosmosdb-sql
77
ms.topic: conceptual
8-
ms.date: 10/25/2021
8+
ms.date: 04/27/2022
99
ms.author: tisande
1010

1111
---
@@ -111,110 +111,18 @@ This query retrieves the family `id` in ascending order of the city name. If mul
111111

112112
## Documents with missing fields
113113

114-
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.
114+
Queries with `ORDER BY` will return all items, including items where the property in the ORDER BY clause isn't defined.
115115

116-
For example, here's a container with an indexing policy that does not explicitly include any paths besides `"/*"`:
117-
118-
```json
119-
{
120-
"indexingMode": "consistent",
121-
"automatic": true,
122-
"includedPaths": [
123-
{
124-
"path": "/*"
125-
}
126-
],
127-
"excludedPaths": []
128-
}
129-
```
130-
131-
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.
132-
133-
Here is a query that sorts by `lastName` on two documents, one of which does not have a `lastName` defined:
116+
For example, if you run the below query that includes `lastName` in the `Order By` clause, the results will include all items, even those that don't have a `lastName` property defined.
134117

135118
```sql
136119
SELECT f.id, f.lastName
137120
FROM Families f
138121
ORDER BY f.lastName
139122
```
140123

141-
The results only include the document that has a defined `lastName`:
142-
143-
```json
144-
[
145-
{
146-
"id": "AndersenFamily",
147-
"lastName": "Andersen"
148-
}
149-
]
150-
```
151-
152-
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. If your `Order By` query uses a [composite index](../index-policy.md#composite-indexes), the results will always include documents with an undefined sort property in the query results.
153-
154-
Here is a sample indexing policy which allows you to have documents with an undefined `lastName` appear in the query results:
155-
156-
```json
157-
{
158-
"indexingMode": "consistent",
159-
"automatic": true,
160-
"includedPaths": [
161-
{
162-
"path": "/lastName/?"
163-
},
164-
{
165-
"path": "/*"
166-
}
167-
],
168-
"excludedPaths": []
169-
}
170-
```
171-
172-
If you run the same query again, documents that are missing `lastName` appear first in the query results:
173-
174-
```sql
175-
SELECT f.id, f.lastName
176-
FROM Families f
177-
ORDER BY f.lastName
178-
```
179-
180-
The results are:
181-
182-
```json
183-
[
184-
{
185-
"id": "WakefieldFamily"
186-
},
187-
{
188-
"id": "AndersenFamily",
189-
"lastName": "Andersen"
190-
}
191-
]
192-
```
193-
194-
If you modify the sort order to `DESC`, documents that are missing `lastName` appear last in the query results:
195-
196-
```sql
197-
SELECT f.id, f.lastName
198-
FROM Families f
199-
ORDER BY f.lastName DESC
200-
```
201-
202-
The results are:
203-
204-
```json
205-
[
206-
{
207-
"id": "AndersenFamily",
208-
"lastName": "Andersen"
209-
},
210-
{
211-
"id": "WakefieldFamily"
212-
}
213-
]
214-
```
215-
216124
> [!Note]
217-
> Only the .NET SDK version 3.4.0 or later supports ORDER BY with mixed types. Therefore, if you want to sort by a combination of undefined and defined values, you should use this version (or later).
125+
> Only the .NET SDK 3.4.0 or later and Java SDK 4.13.0 or later support ORDER BY with mixed types. Therefore, if you want to sort by a combination of undefined and defined values, you should use this version (or later).
218126
219127
You can't control the order that different types appear in the results. In the above example, we showed how undefined values were sorted before string values. If instead, for example, you wanted more control over the sort order of undefined values, you could assign any undefined properties a string value of "aaaaaaaaa" or "zzzzzzzz" to ensure they were either first or last.
220128

0 commit comments

Comments
 (0)