|
1 |
| -import { Aspect, defineAspects, Hint } from './operation'; |
| 1 | +import { Aspect, defineAspects } from './operation'; |
2 | 2 | import { CommandOperation, CommandOperationOptions } from './command';
|
3 |
| -import type { Callback } from '../utils'; |
| 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 | +import type { MongoError } from '../error'; |
8 | 9 |
|
9 | 10 | /** @public */
|
10 | 11 | export interface EstimatedDocumentCountOptions extends CommandOperationOptions {
|
11 |
| - skip?: number; |
12 |
| - limit?: number; |
13 |
| - hint?: Hint; |
| 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 | + */ |
| 17 | + maxTimeMS?: number; |
14 | 18 | }
|
15 | 19 |
|
16 | 20 | /** @internal */
|
17 | 21 | export class EstimatedDocumentCountOperation extends CommandOperation<number> {
|
18 | 22 | options: EstimatedDocumentCountOptions;
|
19 | 23 | collectionName: string;
|
20 |
| - query?: Document; |
21 |
| - |
22 |
| - constructor(collection: Collection, options: EstimatedDocumentCountOptions); |
23 |
| - constructor(collection: Collection, query: Document, options: EstimatedDocumentCountOptions); |
24 |
| - constructor( |
25 |
| - collection: Collection, |
26 |
| - query?: Document | EstimatedDocumentCountOptions, |
27 |
| - options?: EstimatedDocumentCountOptions |
28 |
| - ) { |
29 |
| - if (typeof options === 'undefined') { |
30 |
| - options = query as EstimatedDocumentCountOptions; |
31 |
| - query = undefined; |
32 |
| - } |
33 | 24 |
|
| 25 | + constructor(collection: Collection, options: EstimatedDocumentCountOptions = {}) { |
34 | 26 | super(collection, options);
|
35 | 27 | this.options = options;
|
36 | 28 | this.collectionName = collection.collectionName;
|
37 |
| - if (query) { |
38 |
| - this.query = query; |
39 |
| - } |
40 | 29 | }
|
41 | 30 |
|
42 | 31 | execute(server: Server, session: ClientSession, callback: Callback<number>): void {
|
43 |
| - const options = this.options; |
44 |
| - const cmd: Document = { count: this.collectionName }; |
45 |
| - |
46 |
| - if (this.query) { |
47 |
| - cmd.query = this.query; |
| 32 | + if (maxWireVersion(server) < 12) { |
| 33 | + return this.executeLegacy(server, session, callback); |
48 | 34 | }
|
| 35 | + const pipeline = [{ $collStats: { count: {} } }, { $group: { _id: 1, n: { $sum: '$count' } } }]; |
49 | 36 |
|
50 |
| - if (typeof options.skip === 'number') { |
51 |
| - cmd.skip = options.skip; |
52 |
| - } |
| 37 | + const cmd: Document = { aggregate: this.collectionName, pipeline, cursor: {} }; |
53 | 38 |
|
54 |
| - if (typeof options.limit === 'number') { |
55 |
| - cmd.limit = options.limit; |
| 39 | + if (typeof this.options.maxTimeMS === 'number') { |
| 40 | + cmd.maxTimeMS = this.options.maxTimeMS; |
56 | 41 | }
|
57 | 42 |
|
58 |
| - if (options.hint) { |
59 |
| - cmd.hint = options.hint; |
| 43 | + super.executeCommand(server, session, cmd, (err, response) => { |
| 44 | + if (err && (err as MongoError).code !== 26) { |
| 45 | + callback(err); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + callback(undefined, response?.cursor?.firstBatch[0]?.n || 0); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + executeLegacy(server: Server, session: ClientSession, callback: Callback<number>): void { |
| 54 | + const cmd: Document = { count: this.collectionName }; |
| 55 | + |
| 56 | + if (typeof this.options.maxTimeMS === 'number') { |
| 57 | + cmd.maxTimeMS = this.options.maxTimeMS; |
60 | 58 | }
|
61 | 59 |
|
62 | 60 | super.executeCommand(server, session, cmd, (err, response) => {
|
|
0 commit comments