@@ -131,7 +131,18 @@ public static SortDefinition<TDocument> MetaTextScore<TDocument>(this SortDefini
131
131
public sealed class SortDefinitionBuilder < TDocument >
132
132
{
133
133
/// <summary>
134
- /// Creates an ascending sort.
134
+ /// Creates an ascending sort on a value rather than on a field of a document. For example, "$sort : 1".
135
+ /// This is used when sorting primitive values like strings or numbers, but can also be used to sort whole documents.
136
+ /// </summary>
137
+ /// <returns>A value ascending sort.</returns>
138
+ public SortDefinition < TDocument > Ascending ( )
139
+ {
140
+ return new ValueDirectionalSortDefinition < TDocument > ( SortDirection . Ascending ) ;
141
+ }
142
+
143
+ /// <summary>
144
+ /// Creates an ascending sort based on a specific field within the document. For example, "$sort : { field : 1 }".
145
+ /// This is used when values are documents, and you want to sort by a particular field's value.
135
146
/// </summary>
136
147
/// <param name="field">The field.</param>
137
148
/// <returns>An ascending sort.</returns>
@@ -141,7 +152,8 @@ public SortDefinition<TDocument> Ascending(FieldDefinition<TDocument> field)
141
152
}
142
153
143
154
/// <summary>
144
- /// Creates an ascending sort.
155
+ /// Creates an ascending sort based on a specific field within the document. For example, "$sort : { field : 1 }".
156
+ /// This is used when values are documents, and you want to sort by a particular field's value.
145
157
/// </summary>
146
158
/// <param name="field">The field.</param>
147
159
/// <returns>An ascending sort.</returns>
@@ -171,7 +183,18 @@ public SortDefinition<TDocument> Combine(IEnumerable<SortDefinition<TDocument>>
171
183
}
172
184
173
185
/// <summary>
174
- /// Creates a descending sort.
186
+ /// Creates a descending sort on a value rather than on a field of a document. For example, "$sort : -1".
187
+ /// This is used when sorting primitive values like strings or numbers, but can also be used to sort whole documents.
188
+ /// </summary>
189
+ /// <returns>A value descending sort.</returns>
190
+ public SortDefinition < TDocument > Descending ( )
191
+ {
192
+ return new ValueDirectionalSortDefinition < TDocument > ( SortDirection . Descending ) ;
193
+ }
194
+
195
+ /// <summary>
196
+ /// Creates a descending sort based on a specific field within the document. For example, "$sort: { field: -1 }".
197
+ /// This is used when values are documents, and you want to sort by a particular field's value.
175
198
/// </summary>
176
199
/// <param name="field">The field.</param>
177
200
/// <returns>A descending sort.</returns>
@@ -181,7 +204,8 @@ public SortDefinition<TDocument> Descending(FieldDefinition<TDocument> field)
181
204
}
182
205
183
206
/// <summary>
184
- /// Creates a descending sort.
207
+ /// Creates a descending sort based on a specific field within the document. For example, "$sort: { field: -1 }".
208
+ /// This is used when values are documents, and you want to sort by a particular field's value.
185
209
/// </summary>
186
210
/// <param name="field">The field.</param>
187
211
/// <returns>A descending sort.</returns>
@@ -232,6 +256,11 @@ internal sealed class CombinedSortDefinition<TDocument> : SortDefinition<TDocume
232
256
public CombinedSortDefinition ( IEnumerable < SortDefinition < TDocument > > sorts )
233
257
{
234
258
_sorts = Ensure . IsNotNull ( sorts , nameof ( sorts ) ) . ToList ( ) ;
259
+
260
+ if ( _sorts . Any ( sort => sort is ValueDirectionalSortDefinition < TDocument > ) )
261
+ {
262
+ throw new InvalidOperationException ( "Value-based sort cannot be combined with other sorts. When sorting by the entire element value, no other sorting criteria can be applied." ) ;
263
+ }
235
264
}
236
265
237
266
public override BsonDocument Render ( RenderArgs < TDocument > args )
@@ -272,20 +301,25 @@ public override BsonDocument Render(RenderArgs<TDocument> args)
272
301
{
273
302
var renderedField = _field . Render ( args ) ;
274
303
275
- BsonValue value ;
276
- switch ( _direction )
277
- {
278
- case SortDirection . Ascending :
279
- value = 1 ;
280
- break ;
281
- case SortDirection . Descending :
282
- value = - 1 ;
283
- break ;
284
- default :
285
- throw new InvalidOperationException ( "Unknown value for " + typeof ( SortDirection ) + "." ) ;
286
- }
304
+ return new BsonDocument ( renderedField . FieldName , _direction . Render ( ) ) ;
305
+ }
306
+ }
287
307
288
- return new BsonDocument ( renderedField . FieldName , value ) ;
308
+ internal sealed class ValueDirectionalSortDefinition < TDocument > : SortDefinition < TDocument >
309
+ {
310
+ private readonly SortDirection _direction ;
311
+
312
+ public ValueDirectionalSortDefinition ( SortDirection direction )
313
+ {
314
+ _direction = direction ;
315
+ }
316
+
317
+ public override BsonDocument Render ( RenderArgs < TDocument > args )
318
+ {
319
+ throw new InvalidOperationException (
320
+ "Value-based sort cannot be rendered as a document. You might be trying to use a value-based sort where a field-based sort is expected." ) ;
289
321
}
322
+
323
+ internal override BsonValue RenderAsBsonValue ( RenderArgs < TDocument > args ) => _direction . Render ( ) ;
290
324
}
291
325
}
0 commit comments