Skip to content

Commit a39cea6

Browse files
estimateddocumentcount
1 parent e71504b commit a39cea6

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/operations/estimated_document_count.ts

Lines changed: 14 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,13 @@ export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
1616
maxTimeMS?: number;
1717
}
1818

19+
class EstimatedDocumentCountResponse extends MongoDBResponse {
20+
n = this.getNumber('n') ?? 0;
21+
}
22+
1923
/** @internal */
20-
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
24+
export class EstimatedDocumentCountOperation extends ModernizedCommandOperation<number> {
25+
override SERVER_COMMAND_RESPONSE_TYPE = EstimatedDocumentCountResponse;
2126
override options: EstimatedDocumentCountOptions;
2227
collectionName: string;
2328

@@ -31,11 +36,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
3136
return 'count' as const;
3237
}
3338

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

4142
if (typeof this.options.maxTimeMS === 'number') {
@@ -48,9 +49,11 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
4849
cmd.comment = this.options.comment;
4950
}
5051

51-
const response = await super.executeCommand(server, session, cmd, timeoutContext);
52+
return cmd;
53+
}
5254

53-
return response?.n || 0;
55+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
56+
return response.n;
5457
}
5558
}
5659

0 commit comments

Comments
 (0)