@@ -7,21 +7,14 @@ import { Logger } from '../logger';
7
7
import { executeOperation } from '../operations/execute_operation' ;
8
8
import { CountOperation , CountOptions } from '../operations/count' ;
9
9
import { ReadPreference , ReadPreferenceLike } from '../read_preference' ;
10
- import {
11
- Callback ,
12
- emitDeprecatedOptionWarning ,
13
- formattedOrderClause ,
14
- maybePromise ,
15
- MongoDBNamespace
16
- } from '../utils' ;
17
-
10
+ import { Callback , emitDeprecatedOptionWarning , maybePromise , MongoDBNamespace } from '../utils' ;
11
+ import { Sort , SortDirection , formatSort } from '../sort' ;
18
12
import type { OperationTime , ResumeToken } from '../change_stream' ;
19
13
import type { CloseOptions } from '../cmap/connection_pool' ;
20
14
import type { CollationOptions } from '../cmap/wire_protocol/write_command' ;
15
+ import type { Hint , OperationBase } from '../operations/operation' ;
21
16
import type { Topology } from '../sdam/topology' ;
22
17
import type { CommandOperationOptions } from '../operations/command' ;
23
- import type { Sort , SortDirection } from '../operations/find' ;
24
- import type { Hint , OperationBase } from '../operations/operation' ;
25
18
import type { ReadConcern } from '../read_concern' ;
26
19
import type { Server } from '../sdam/server' ;
27
20
import type { ClientSession } from '../sessions' ;
@@ -99,6 +92,7 @@ export interface CursorOptions extends CommandOperationOptions {
99
92
topology ?: Topology ;
100
93
/** Session to use for the operation */
101
94
numberOfRetries ?: number ;
95
+ sort ?: Sort ;
102
96
}
103
97
104
98
/** @public */
@@ -399,6 +393,10 @@ export class Cursor<
399
393
this . addCursorFlag ( 'noCursorTimeout' , true ) ;
400
394
}
401
395
396
+ if ( this . options . sort ) {
397
+ this . cmd . sort = formatSort ( this . options . sort ) ;
398
+ }
399
+
402
400
// Set the batch size
403
401
this . _batchSize = batchSize ;
404
402
}
@@ -688,14 +686,6 @@ export class Cursor<
688
686
return ;
689
687
}
690
688
691
- if ( this . s . state === CursorState . INIT && this . cmd . sort ) {
692
- try {
693
- this . cmd . sort = formattedOrderClause ( this . cmd . sort ) ;
694
- } catch ( err ) {
695
- return cb ( err ) ;
696
- }
697
- }
698
-
699
689
nextFunction ( this , ( err , doc ) => {
700
690
if ( err ) return cb ( err ) ;
701
691
this . s . state = CursorState . OPEN ;
@@ -940,37 +930,7 @@ export class Cursor<
940
930
throw new MongoError ( 'Cursor is closed' ) ;
941
931
}
942
932
943
- let order = sort ;
944
-
945
- // We have an array of arrays, we need to preserve the order of the sort
946
- // so we will us a Map
947
- if ( Array . isArray ( order ) && Array . isArray ( order [ 0 ] ) ) {
948
- this . cmd . sort = new Map < string , unknown > (
949
- ( order as [ string , SortDirection ] [ ] ) . map ( ( [ key , dir ] ) => {
950
- if ( dir === 'asc' ) {
951
- return [ key , 1 ] ;
952
- } else if ( dir === 'desc' ) {
953
- return [ key , - 1 ] ;
954
- } else if ( dir === 1 || dir === - 1 || dir . $meta ) {
955
- return [ key , dir ] ;
956
- } else {
957
- throw new MongoError (
958
- "Illegal sort clause, must be of the form [['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]"
959
- ) ;
960
- }
961
-
962
- return [ key , null ] ;
963
- } )
964
- ) ;
965
-
966
- return this ;
967
- }
968
-
969
- if ( direction != null ) {
970
- order = [ [ sort as string , direction ] ] ;
971
- }
972
-
973
- this . cmd . sort = order ;
933
+ this . cmd . sort = formatSort ( sort , direction ) ;
974
934
return this ;
975
935
}
976
936
0 commit comments