1- import type { Document } from '../bson' ;
1+ import { BSONType , type Document , parseUtf8ValidationOption } from '../bson' ;
22import { type Connection } from '../cmap/connection' ;
3- import { DistinctResponse } from '../cmap/wire_protocol/responses' ;
3+ import { MongoDBResponse } from '../cmap/wire_protocol/responses' ;
44import type { Collection } from '../collection' ;
55import { type CommandOperationOptions , ModernizedCommandOperation } from './command' ;
66import { Aspect , defineAspects } from './operation' ;
@@ -21,11 +21,17 @@ export type DistinctOptions = CommandOperationOptions & {
2121 hint ?: Document | string ;
2222} ;
2323
24+ class DistinctResponse extends MongoDBResponse {
25+ get values ( ) {
26+ return this . get ( 'values' , BSONType . array , true ) ;
27+ }
28+ }
29+
2430/**
2531 * Return a list of distinct values for the given key across a collection.
2632 * @internal
2733 */
28- export class DistinctOperation extends ModernizedCommandOperation < any [ ] > {
34+ export class DistinctOperation extends ModernizedCommandOperation < any [ ] | Document > {
2935 override SERVER_COMMAND_RESPONSE_TYPE = DistinctResponse ;
3036 override options : DistinctOptions ;
3137 collection : Collection ;
@@ -63,8 +69,17 @@ export class DistinctOperation extends ModernizedCommandOperation<any[]> {
6369 } ;
6470 }
6571
66- override handleOk ( response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE > ) : any [ ] {
67- this . explain ? response . toObject ( ) : response . values ;
72+ override handleOk (
73+ response : InstanceType < typeof this . SERVER_COMMAND_RESPONSE_TYPE >
74+ ) : any [ ] | Document {
75+ if ( this . explain ) {
76+ return response . toObject ( this . bsonOptions ) ;
77+ }
78+ const values = response . values . toObject ( {
79+ ...this . bsonOptions ,
80+ validation : parseUtf8ValidationOption ( this . bsonOptions )
81+ } ) ;
82+ return Object . entries ( values ) . map ( ( [ _key , val ] ) => val ) ;
6883 }
6984}
7085
0 commit comments