You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/cosmos-db/sql/sql-query-order-by.md
+4-96Lines changed: 4 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ author: timsander1
5
5
ms.service: cosmos-db
6
6
ms.subservice: cosmosdb-sql
7
7
ms.topic: conceptual
8
-
ms.date: 10/25/2021
8
+
ms.date: 04/27/2022
9
9
ms.author: tisande
10
10
11
11
---
@@ -111,110 +111,18 @@ This query retrieves the family `id` in ascending order of the city name. If mul
111
111
112
112
## Documents with missing fields
113
113
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.
115
115
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.
134
117
135
118
```sql
136
119
SELECTf.id, f.lastName
137
120
FROM Families f
138
121
ORDER BYf.lastName
139
122
```
140
123
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
-
SELECTf.id, f.lastName
176
-
FROM Families f
177
-
ORDER BYf.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
-
SELECTf.id, f.lastName
198
-
FROM Families f
199
-
ORDER BYf.lastNameDESC
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
-
216
124
> [!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).
218
126
219
127
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.
0 commit comments