Skip to content

Commit 0f781a8

Browse files
committed
fix: distinct typing
1 parent b171c3e commit 0f781a8

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

src/collection.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from './cursor/list_search_indexes_cursor';
1818
import type { Db } from './db';
1919
import { MongoAPIError, MongoInvalidArgumentError, MongoOperationTimeoutError } from './error';
20+
import { type ExplainCommandOptions, type ExplainVerbosityLike } from './explain';
2021
import type { MongoClient, PkFactory } from './mongo_client';
2122
import type {
2223
Abortable,
@@ -853,31 +854,32 @@ export class Collection<TSchema extends Document = Document> {
853854
*/
854855
distinct<Key extends keyof WithId<TSchema>>(
855856
key: Key
856-
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
857+
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
857858
distinct<Key extends keyof WithId<TSchema>>(
858859
key: Key,
859860
filter: Filter<TSchema>
860-
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
861+
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
861862
distinct<Key extends keyof WithId<TSchema>>(
862863
key: Key,
863864
filter: Filter<TSchema>,
864865
options: DistinctOptions
865-
): Promise<Array<Flatten<WithId<TSchema>[Key]>> | Document>;
866+
): Promise<Array<Flatten<WithId<TSchema>[Key]>>>;
867+
distinct<Key extends keyof WithId<TSchema>>(
868+
key: Key,
869+
filter: Filter<TSchema>,
870+
options: DistinctOptions & { explain: ExplainVerbosityLike | ExplainCommandOptions }
871+
): Promise<Document>;
866872

867873
// Embedded documents overload
868-
distinct(key: string): Promise<any[] | Document>;
869-
distinct(key: string, filter: Filter<TSchema>): Promise<any[] | Document>;
870-
distinct(
871-
key: string,
872-
filter: Filter<TSchema>,
873-
options: DistinctOptions
874-
): Promise<any[] | Document>;
874+
distinct(key: string): Promise<any[]>;
875+
distinct(key: string, filter: Filter<TSchema>): Promise<any[]>;
876+
distinct(key: string, filter: Filter<TSchema>, options: DistinctOptions): Promise<any[]>;
875877

876878
async distinct<Key extends keyof WithId<TSchema>>(
877879
key: Key,
878880
filter: Filter<TSchema> = {},
879881
options: DistinctOptions = {}
880-
): Promise<any[] | Document> {
882+
): Promise<any[]> {
881883
return await executeOperation(
882884
this.client,
883885
new DistinctOperation(

src/operations/distinct.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BSONType, type Document, parseUtf8ValidationOption } from '../bson';
1+
import { type Document } from '../bson';
22
import { type Connection } from '../cmap/connection';
33
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
44
import type { Collection } from '../collection';
@@ -21,18 +21,12 @@ 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-
3024
/**
3125
* Return a list of distinct values for the given key across a collection.
3226
* @internal
3327
*/
3428
export class DistinctOperation extends ModernizedCommandOperation<any[] | Document> {
35-
override SERVER_COMMAND_RESPONSE_TYPE = DistinctResponse;
29+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
3630
override options: DistinctOptions;
3731
collection: Collection;
3832
/** Field of the document to find distinct values for. */
@@ -75,11 +69,7 @@ export class DistinctOperation extends ModernizedCommandOperation<any[] | Docume
7569
if (this.explain) {
7670
return response.toObject(this.bsonOptions);
7771
}
78-
const values = response.values.toObject({
79-
...this.bsonOptions,
80-
validation: parseUtf8ValidationOption(this.bsonOptions)
81-
});
82-
return Object.entries(values).map(([_key, val]) => val);
72+
return response.toObject(this.bsonOptions).values;
8373
}
8474
}
8575

0 commit comments

Comments
 (0)