1- import type { Document } from '../bson' ;
1+ import { type Document } from '../bson' ;
2+ import { type Connection } from '../cmap/connection' ;
3+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
24import type { Collection } from '../collection' ;
3- import type { Server } from '../sdam/server' ;
4- import type { ClientSession } from '../sessions' ;
5- import { type TimeoutContext } from '../timeout' ;
6- import { decorateWithCollation , decorateWithReadConcern } from '../utils' ;
7- import { CommandOperation , type CommandOperationOptions } from './command' ;
5+ import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
86import { Aspect , defineAspects } from './operation' ;
97
108/** @public */
@@ -27,7 +25,8 @@ export type DistinctOptions = CommandOperationOptions & {
2725 * Return a list of distinct values for the given key across a collection.
2826 * @internal
2927 */
30- export class DistinctOperation extends CommandOperation < any [ ] > {
28+ export class DistinctOperation extends ModernizedCommandOperation < any [ ] | Document > {
29+ override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse ;
3130 override options : DistinctOptions ;
3231 collection : Collection ;
3332 /** Field of the document to find distinct values for. */
@@ -56,48 +55,32 @@ export class DistinctOperation extends CommandOperation<any[]> {
5655 return 'distinct' as const ;
5756 }
5857
59- override async execute (
60- server : Server ,
61- session : ClientSession | undefined ,
62- timeoutContext : TimeoutContext
63- ) : Promise < any [ ] > {
64- const coll = this . collection ;
65- const key = this . key ;
66- const query = this . query ;
67- const options = this . options ;
68-
69- // Distinct command
70- const cmd : Document = {
71- distinct : coll . collectionName ,
72- key : key ,
73- query : query
58+ override buildCommandDocument ( _connection : Connection ) : Document {
59+ const command : Document = {
60+ distinct : this . collection . collectionName ,
61+ key : this . key ,
62+ query : this . query
7463 } ;
75-
76- // Add maxTimeMS if defined
77- if ( typeof options . maxTimeMS === 'number' ) {
78- cmd . maxTimeMS = options . maxTimeMS ;
79- }
80-
8164 // we check for undefined specifically here to allow falsy values
8265 // eslint-disable-next-line no-restricted-syntax
83- if ( typeof options . comment !== ' undefined' ) {
84- cmd . comment = options . comment ;
66+ if ( this . options . comment !== undefined ) {
67+ command . comment = this . options . comment ;
8568 }
8669
87- if ( options . hint != null ) {
88- cmd . hint = options . hint ;
70+ if ( this . options . hint != null ) {
71+ command . hint = this . options . hint ;
8972 }
9073
91- // Do we have a readConcern specified
92- decorateWithReadConcern ( cmd , coll , options ) ;
93-
94- // Have we specified collation
95- decorateWithCollation ( cmd , coll , options ) ;
96-
97- const result = await super . executeCommand ( server , session , cmd , timeoutContext ) ;
74+ return command ;
75+ }
9876
99- // @ts -expect-error: Explain always returns a document
100- return this . explain ? result : result . values ;
77+ override handleOk (
78+ response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE >
79+ ) : any [ ] | Document {
80+ if ( this . explain ) {
81+ return response . toObject ( this . bsonOptions ) ;
82+ }
83+ return response . toObject ( this . bsonOptions ) . values ;
10184 }
10285}
10386
0 commit comments