opusdev/vector-similarity-api
1
1import { type Document } from '../bson';2import { type Connection } from '../cmap/connection';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import type { Collection } from '../collection';5import { CommandOperation, type CommandOperationOptions } from './command';6import { Aspect, defineAspects } from './operation';7 8/** @public */9export type DistinctOptions = CommandOperationOptions & {10 /**11 * @sinceServerVersion 7.112 *13 * The index to use. Specify either the index name as a string or the index key pattern.14 * If specified, then the query system will only consider plans using the hinted index.15 *16 * If provided as a string, `hint` must be index name for an index on the collection.17 * If provided as an object, `hint` must be an index description for an index defined on the collection.18 *19 * See https://www.mongodb.com/docs/manual/reference/command/distinct/#command-fields.20 */21 hint?: Document | string;22};23 24/**25 * Return a list of distinct values for the given key across a collection.26 * @internal27 */28export class DistinctOperation extends CommandOperation<any[] | Document> {29 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;30 override options: DistinctOptions;31 collection: Collection;32 /** Field of the document to find distinct values for. */33 key: string;34 /** The query for filtering the set of documents to which we apply the distinct filter. */35 query: Document;36 37 /**38 * Construct a Distinct operation.39 *40 * @param collection - Collection instance.41 * @param key - Field of the document to find distinct values for.42 * @param query - The query for filtering the set of documents to which we apply the distinct filter.43 * @param options - Optional settings. See Collection.prototype.distinct for a list of options.44 */45 constructor(collection: Collection, key: string, query: Document, options?: DistinctOptions) {46 super(collection, options);47 48 this.options = options ?? {};49 this.collection = collection;50 this.key = key;51 this.query = query;52 }53 54 override get commandName() {55 return 'distinct' as const;56 }57 58 override buildCommandDocument(_connection: Connection): Document {59 const command: Document = {60 distinct: this.collection.collectionName,61 key: this.key,62 query: this.query63 };64 // we check for undefined specifically here to allow falsy values65 // eslint-disable-next-line no-restricted-syntax66 if (this.options.comment !== undefined) {67 command.comment = this.options.comment;68 }69 70 if (this.options.hint != null) {71 command.hint = this.options.hint;72 }73 74 return command;75 }76 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;84 }85}86 87defineAspects(DistinctOperation, [88 Aspect.READ_OPERATION,89 Aspect.RETRYABLE,90 Aspect.EXPLAINABLE,91 Aspect.SUPPORTS_RAW_DATA92]);93 