Skip to content

Commit f93b4f7

Browse files
committed
first pass implementation
1 parent 53679aa commit f93b4f7

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/operations/estimated_document_count.ts

+42-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
import { Aspect, defineAspects, Hint } from './operation';
22
import { CommandOperation, CommandOperationOptions } from './command';
3-
import type { Callback } from '../utils';
3+
import { Callback, maxWireVersion } from '../utils';
44
import type { Document } from '../bson';
55
import type { Server } from '../sdam/server';
66
import type { Collection } from '../collection';
77
import type { ClientSession } from '../sessions';
8+
import { MongoError } from '../error';
89

9-
/** @public */
10-
export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
10+
/**
11+
* All supported options, including legacy options
12+
* @public
13+
*/
14+
export interface EstimatedDocumentCountOptions extends EstimatedDocumentCountOptionsV1 {
1115
skip?: number;
1216
limit?: number;
1317
hint?: Hint;
1418
}
1519

20+
/**
21+
* Options supported by Server API Version 1
22+
* @public
23+
*/
24+
export interface EstimatedDocumentCountOptionsV1 extends CommandOperationOptions {
25+
/** specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. */
26+
maxTimeMS?: number;
27+
}
28+
1629
/** @internal */
1730
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
1831
options: EstimatedDocumentCountOptions;
@@ -41,22 +54,36 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
4154

4255
execute(server: Server, session: ClientSession, callback: Callback<number>): void {
4356
const options = this.options;
44-
const cmd: Document = { count: this.collectionName };
4557

46-
if (this.query) {
47-
cmd.query = this.query;
48-
}
58+
let cmd: Document;
4959

50-
if (typeof options.skip === 'number') {
51-
cmd.skip = options.skip;
52-
}
60+
if (maxWireVersion(server) > 11) {
61+
const pipeline = [
62+
{ $collStats: { count: {} } },
63+
{ $group: { _id: 1, n: { $sum: '$count' } } }
64+
];
65+
cmd = { aggregate: this.collectionName, pipeline, cursor: {} };
66+
if (typeof options.maxTimeMS === 'number') {
67+
cmd.maxTimeMS = options.maxTimeMS;
68+
}
69+
} else {
70+
cmd = { count: this.collectionName };
5371

54-
if (typeof options.limit === 'number') {
55-
cmd.limit = options.limit;
56-
}
72+
if (this.query) {
73+
cmd.query = this.query;
74+
}
75+
76+
if (typeof options.skip === 'number') {
77+
cmd.skip = options.skip;
78+
}
5779

58-
if (options.hint) {
59-
cmd.hint = options.hint;
80+
if (typeof options.limit === 'number') {
81+
cmd.limit = options.limit;
82+
}
83+
84+
if (options.hint) {
85+
cmd.hint = options.hint;
86+
}
6087
}
6188

6289
super.executeCommand(server, session, cmd, (err, response) => {

0 commit comments

Comments
 (0)