@@ -131,7 +131,18 @@ public static SortDefinition<TDocument> MetaTextScore<TDocument>(this SortDefini
131131 public sealed class SortDefinitionBuilder < TDocument >
132132 {
133133 /// <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.
135146 /// </summary>
136147 /// <param name="field">The field.</param>
137148 /// <returns>An ascending sort.</returns>
@@ -141,7 +152,8 @@ public SortDefinition<TDocument> Ascending(FieldDefinition<TDocument> field)
141152 }
142153
143154 /// <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.
145157 /// </summary>
146158 /// <param name="field">The field.</param>
147159 /// <returns>An ascending sort.</returns>
@@ -171,7 +183,18 @@ public SortDefinition<TDocument> Combine(IEnumerable<SortDefinition<TDocument>>
171183 }
172184
173185 /// <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.
175198 /// </summary>
176199 /// <param name="field">The field.</param>
177200 /// <returns>A descending sort.</returns>
@@ -181,7 +204,8 @@ public SortDefinition<TDocument> Descending(FieldDefinition<TDocument> field)
181204 }
182205
183206 /// <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.
185209 /// </summary>
186210 /// <param name="field">The field.</param>
187211 /// <returns>A descending sort.</returns>
@@ -232,6 +256,11 @@ internal sealed class CombinedSortDefinition<TDocument> : SortDefinition<TDocume
232256 public CombinedSortDefinition ( IEnumerable < SortDefinition < TDocument > > sorts )
233257 {
234258 _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+ }
235264 }
236265
237266 public override BsonDocument Render ( RenderArgs < TDocument > args )
@@ -272,20 +301,25 @@ public override BsonDocument Render(RenderArgs<TDocument> args)
272301 {
273302 var renderedField = _field . Render ( args ) ;
274303
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+ }
287307
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." ) ;
289321 }
322+
323+ internal override BsonValue RenderAsBsonValue ( RenderArgs < TDocument > args ) => _direction . Render ( ) ;
290324 }
291325}
0 commit comments