Skip to content

Commit 669a089

Browse files
count
1 parent e628296 commit 669a089

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src/operations/count.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
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';
66
import type { MongoDBNamespace } from '../utils';
7-
import { CommandOperation, type CommandOperationOptions } from './command';
7+
import { type CommandOperationOptions, ModernizedCommandOperation } from './command';
88
import { Aspect, defineAspects } from './operation';
99

1010
/** @public */
@@ -21,8 +21,15 @@ export interface CountOptions extends CommandOperationOptions {
2121
hint?: string | Document;
2222
}
2323

24+
class CountResponse extends MongoDBResponse {
25+
get n(): number {
26+
return this.getNumber('n') ?? 0;
27+
}
28+
}
29+
2430
/** @internal */
25-
export class CountOperation extends CommandOperation<number> {
31+
export class CountOperation extends ModernizedCommandOperation<number> {
32+
override SERVER_COMMAND_RESPONSE_TYPE = CountResponse;
2633
override options: CountOptions;
2734
collectionName?: string;
2835
query: Document;
@@ -39,35 +46,33 @@ export class CountOperation extends CommandOperation<number> {
3946
return 'count' as const;
4047
}
4148

42-
override async execute(
43-
server: Server,
44-
session: ClientSession | undefined,
45-
timeoutContext: TimeoutContext
46-
): Promise<number> {
47-
const options = this.options;
49+
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
4850
const cmd: Document = {
4951
count: this.collectionName,
5052
query: this.query
5153
};
5254

53-
if (typeof options.limit === 'number') {
54-
cmd.limit = options.limit;
55+
if (typeof this.options.limit === 'number') {
56+
cmd.limit = this.options.limit;
5557
}
5658

57-
if (typeof options.skip === 'number') {
58-
cmd.skip = options.skip;
59+
if (typeof this.options.skip === 'number') {
60+
cmd.skip = this.options.skip;
5961
}
6062

61-
if (options.hint != null) {
62-
cmd.hint = options.hint;
63+
if (this.options.hint != null) {
64+
cmd.hint = this.options.hint;
6365
}
6466

65-
if (typeof options.maxTimeMS === 'number') {
66-
cmd.maxTimeMS = options.maxTimeMS;
67+
if (typeof this.options.maxTimeMS === 'number') {
68+
cmd.maxTimeMS = this.options.maxTimeMS;
6769
}
6870

69-
const result = await super.executeCommand(server, session, cmd, timeoutContext);
70-
return result ? result.n : 0;
71+
return cmd;
72+
}
73+
74+
override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): number {
75+
return response.n;
7176
}
7277
}
7378

0 commit comments

Comments
 (0)