Skip to content

Commit 6d52e39

Browse files
estimateddocumentcount
1 parent 669a089 commit 6d52e39

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/operations/estimated_document_count.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { type Connection } from '..';
12
import type { Document } from '../bson';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
24
import type { Collection } from '../collection';
3-
import type { Server } from '../sdam/server';
45
import type { ClientSession } from '../sessions';
5-
import { type TimeoutContext } from '../timeout';
6-
import { CommandOperation, type CommandOperationOptions } from './command';
6+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
77
import { Aspect, defineAspects } from './operation';
88

99
/** @public */
@@ -16,8 +16,15 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
1616
maxTimeMS?: number;
1717
}
1818

19+
class EstimatedDocumentCountResponse extends MongoDBResponse {
20+
get n(): number {
21+
return this.getNumber('n') ?? 0;
22+
}
23+
}
24+
1925
/** @internal */
20-
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
26+
export class EstimatedDocumentCountOperation extends ModernizedCommandOperation<number> {
27+
override SERVER_COMMAND_RESPONSE_TYPE = EstimatedDocumentCountResponse;
2128
override options: EstimatedDocumentCountOptions;
2229
collectionName: string;
2330

@@ -31,11 +38,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
3138
return 'count' as const;
3239
}
3340

34-
override async execute(
35-
server: Server,
36-
session: ClientSession | undefined,
37-
timeoutContext: TimeoutContext
38-
): Promise<number> {
41+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
3942
const cmd: Document = { count: this.collectionName };
4043

4144
if (typeof this.options.maxTimeMS === 'number') {
@@ -48,9 +51,11 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
4851
cmd.comment = this.options.comment;
4952
}
5053

51-
const response = await super.executeCommand(server, session, cmd, timeoutContext);
54+
return cmd;
55+
}
5256

53-
return response?.n || 0;
57+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
58+
return response.n;
5459
}
5560
}
5661

0 commit comments

Comments
 (0)