|
1 | 1 | import { Aspect, defineAspects, Hint } 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 { MongoError } from '../error'; |
8 | 9 |
|
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 { |
11 | 15 | skip?: number;
|
12 | 16 | limit?: number;
|
13 | 17 | hint?: Hint;
|
14 | 18 | }
|
15 | 19 |
|
| 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 | + |
16 | 29 | /** @internal */
|
17 | 30 | export class EstimatedDocumentCountOperation extends CommandOperation<number> {
|
18 | 31 | options: EstimatedDocumentCountOptions;
|
@@ -41,22 +54,36 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
|
41 | 54 |
|
42 | 55 | execute(server: Server, session: ClientSession, callback: Callback<number>): void {
|
43 | 56 | const options = this.options;
|
44 |
| - const cmd: Document = { count: this.collectionName }; |
45 | 57 |
|
46 |
| - if (this.query) { |
47 |
| - cmd.query = this.query; |
48 |
| - } |
| 58 | + let cmd: Document; |
49 | 59 |
|
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 }; |
53 | 71 |
|
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 | + } |
57 | 79 |
|
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 | + } |
60 | 87 | }
|
61 | 88 |
|
62 | 89 | super.executeCommand(server, session, cmd, (err, response) => {
|
|
0 commit comments