Skip to content

Commit b76612f

Browse files
committed
second pass implementation
1 parent e10849e commit b76612f

7 files changed

+33
-983
lines changed

src/collection.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,6 @@ export class Collection {
939939
callback?: Callback<number>
940940
): Promise<number> | void {
941941
if (typeof options === 'function') (callback = options), (options = {});
942-
943942
return executeOperation(
944943
getTopology(this),
945944
new EstimatedDocumentCountOperation(this, resolveOptions(this, options)),
@@ -1430,7 +1429,7 @@ export class Collection {
14301429
query = query || {};
14311430
return executeOperation(
14321431
getTopology(this),
1433-
new EstimatedDocumentCountOperation(this, query, resolveOptions(this, options)),
1432+
new CountDocumentsOperation(this, query, resolveOptions(this, options)),
14341433
callback
14351434
);
14361435
}

src/operations/estimated_document_count.ts

+31-58
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,63 @@
1-
import { Aspect, defineAspects, Hint } from './operation';
1+
import { Aspect, defineAspects } from './operation';
22
import { CommandOperation, CommandOperationOptions } from './command';
33
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-
9-
/**
10-
* All supported options, including legacy options
11-
* @public
12-
*/
13-
export interface EstimatedDocumentCountOptions extends EstimatedDocumentCountOptionsV1 {
14-
skip?: number;
15-
limit?: number;
16-
hint?: Hint;
17-
}
18-
19-
/**
20-
* Options supported by Server API Version 1
21-
* @public
22-
*/
23-
export interface EstimatedDocumentCountOptionsV1 extends CommandOperationOptions {
24-
/** specifies a cumulative time limit in milliseconds for processing operations on the cursor. MongoDB interrupts the operation at the earliest following interrupt point. */
8+
import type { MongoError } from '../error';
9+
10+
/** @public */
11+
export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
12+
/**
13+
* The maximum amount of time to allow the operation to run.
14+
*
15+
* This option is sent only if the caller explicitly provides a value. The default is to not send a value.
16+
*/
2517
maxTimeMS?: number;
2618
}
2719

2820
/** @internal */
2921
export class EstimatedDocumentCountOperation extends CommandOperation<number> {
3022
options: EstimatedDocumentCountOptions;
3123
collectionName: string;
32-
query?: Document;
3324

34-
constructor(collection: Collection, options: EstimatedDocumentCountOptions);
35-
constructor(collection: Collection, query: Document, options: EstimatedDocumentCountOptions);
36-
constructor(
37-
collection: Collection,
38-
query?: Document | EstimatedDocumentCountOptions,
39-
options?: EstimatedDocumentCountOptions
40-
) {
25+
constructor(collection: Collection, options?: EstimatedDocumentCountOptions) {
4126
if (typeof options === 'undefined') {
42-
options = query as EstimatedDocumentCountOptions;
43-
query = undefined;
27+
options = {} as EstimatedDocumentCountOptions;
4428
}
45-
4629
super(collection, options);
4730
this.options = options;
4831
this.collectionName = collection.collectionName;
49-
if (query) {
50-
this.query = query;
51-
}
5232
}
5333

5434
execute(server: Server, session: ClientSession, callback: Callback<number>): void {
55-
const options = this.options;
56-
57-
let cmd: Document;
58-
59-
if (maxWireVersion(server) > 11) {
60-
const pipeline = [
61-
{ $collStats: { count: {} } },
62-
{ $group: { _id: 1, n: { $sum: '$count' } } }
63-
];
35+
if (maxWireVersion(server) < 12) {
36+
return this.executeLegacy(server, session, callback);
37+
}
38+
const pipeline = [{ $collStats: { count: {} } }, { $group: { _id: 1, n: { $sum: '$count' } } }];
6439

65-
cmd = { aggregate: this.collectionName, pipeline, cursor: {} };
40+
const cmd: Document = { aggregate: this.collectionName, pipeline, cursor: {} };
6641

67-
if (typeof options.maxTimeMS === 'number') {
68-
cmd.maxTimeMS = options.maxTimeMS;
69-
}
70-
} else {
71-
cmd = { count: this.collectionName };
42+
if (typeof this.options.maxTimeMS === 'number') {
43+
cmd.maxTimeMS = this.options.maxTimeMS;
44+
}
7245

73-
if (this.query) {
74-
cmd.query = this.query;
46+
super.executeCommand(server, session, cmd, (err, response) => {
47+
if (err && (err as MongoError).code !== 26) {
48+
callback(err);
49+
return;
7550
}
7651

77-
if (typeof options.skip === 'number') {
78-
cmd.skip = options.skip;
79-
}
52+
callback(undefined, response?.cursor?.firstBatch[0]?.n || 0);
53+
});
54+
}
8055

81-
if (typeof options.limit === 'number') {
82-
cmd.limit = options.limit;
83-
}
56+
executeLegacy(server: Server, session: ClientSession, callback: Callback<number>): void {
57+
const cmd: Document = { count: this.collectionName };
8458

85-
if (options.hint) {
86-
cmd.hint = options.hint;
87-
}
59+
if (typeof this.options.maxTimeMS === 'number') {
60+
cmd.maxTimeMS = this.options.maxTimeMS;
8861
}
8962

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

0 commit comments

Comments
 (0)