|
1 |
| -import { Aspect, defineAspects, Hint } from './operation'; |
| 1 | +import { Aspect, defineAspects } from './operation'; |
2 | 2 | import { CommandOperation, CommandOperationOptions } from './command';
|
3 | 3 | import { Callback, maxWireVersion } from '../utils';
|
4 | 4 | import type { Document } from '../bson';
|
5 | 5 | import type { Server } from '../sdam/server';
|
6 | 6 | import type { Collection } from '../collection';
|
7 | 7 | 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 | + */ |
25 | 17 | maxTimeMS?: number;
|
26 | 18 | }
|
27 | 19 |
|
28 | 20 | /** @internal */
|
29 | 21 | export class EstimatedDocumentCountOperation extends CommandOperation<number> {
|
30 | 22 | options: EstimatedDocumentCountOptions;
|
31 | 23 | collectionName: string;
|
32 |
| - query?: Document; |
33 | 24 |
|
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) { |
41 | 26 | if (typeof options === 'undefined') {
|
42 |
| - options = query as EstimatedDocumentCountOptions; |
43 |
| - query = undefined; |
| 27 | + options = {} as EstimatedDocumentCountOptions; |
44 | 28 | }
|
45 |
| - |
46 | 29 | super(collection, options);
|
47 | 30 | this.options = options;
|
48 | 31 | this.collectionName = collection.collectionName;
|
49 |
| - if (query) { |
50 |
| - this.query = query; |
51 |
| - } |
52 | 32 | }
|
53 | 33 |
|
54 | 34 | 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' } } }]; |
64 | 39 |
|
65 |
| - cmd = { aggregate: this.collectionName, pipeline, cursor: {} }; |
| 40 | + const cmd: Document = { aggregate: this.collectionName, pipeline, cursor: {} }; |
66 | 41 |
|
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 | + } |
72 | 45 |
|
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; |
75 | 50 | }
|
76 | 51 |
|
77 |
| - if (typeof options.skip === 'number') { |
78 |
| - cmd.skip = options.skip; |
79 |
| - } |
| 52 | + callback(undefined, response?.cursor?.firstBatch[0]?.n || 0); |
| 53 | + }); |
| 54 | + } |
80 | 55 |
|
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 }; |
84 | 58 |
|
85 |
| - if (options.hint) { |
86 |
| - cmd.hint = options.hint; |
87 |
| - } |
| 59 | + if (typeof this.options.maxTimeMS === 'number') { |
| 60 | + cmd.maxTimeMS = this.options.maxTimeMS; |
88 | 61 | }
|
89 | 62 |
|
90 | 63 | super.executeCommand(server, session, cmd, (err, response) => {
|
|
0 commit comments