Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4079): estimated document count uses count #3244

Merged
merged 9 commits into from
May 18, 2022
2 changes: 2 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,9 @@ export class Collection<TSchema extends Document = Document> {

/**
* Gets an estimate of the count of documents in a collection using collection metadata.
* This will always run a count command on all server versions.
*
* @see {@link https://www.mongodb.com/docs/manual/reference/command/count/#behavior|Count: Behavior}
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
Expand Down
32 changes: 3 additions & 29 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Collection } from '../collection';
import type { MongoServerError } from '../error';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { Callback, maxWireVersion } from '../utils';
import type { Callback } from '../utils';
import { CommandOperation, CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';

Expand Down Expand Up @@ -32,32 +32,6 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
server: Server,
session: ClientSession | undefined,
callback: Callback<number>
): void {
if (maxWireVersion(server) < 12) {
return this.executeLegacy(server, session, callback);
}
const pipeline = [{ $collStats: { count: {} } }, { $group: { _id: 1, n: { $sum: '$count' } } }];

const cmd: Document = { aggregate: this.collectionName, pipeline, cursor: {} };

if (typeof this.options.maxTimeMS === 'number') {
cmd.maxTimeMS = this.options.maxTimeMS;
}

super.executeCommand(server, session, cmd, (err, response) => {
if (err && (err as MongoServerError).code !== 26) {
callback(err);
return;
}

callback(undefined, response?.cursor?.firstBatch[0]?.n || 0);
});
}

executeLegacy(
server: Server,
session: ClientSession | undefined,
callback: Callback<number>
): void {
const cmd: Document = { count: this.collectionName };

Expand All @@ -66,12 +40,12 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
}

super.executeCommand(server, session, cmd, (err, response) => {
if (err) {
if (err && (err as MongoServerError).code !== 26) {
baileympearson marked this conversation as resolved.
Show resolved Hide resolved
callback(err);
return;
}

callback(undefined, response.n || 0);
callback(undefined, response?.n || 0);
});
}
}
Expand Down
19 changes: 2 additions & 17 deletions test/spec/atlas-data-lake-testing/estimatedDocumentCount.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,9 @@
{
"command_started_event": {
"command": {
"aggregate": "driverdata",
"pipeline": [
{
"$collStats": {
"count": {}
}
},
{
"$group": {
"_id": 1,
"n": {
"$sum": "$count"
}
}
}
]
"count": "driverdata"
},
"command_name": "aggregate",
"command_name": "count",
"database_name": "test"
}
}
Expand Down
9 changes: 3 additions & 6 deletions test/spec/atlas-data-lake-testing/estimatedDocumentCount.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ tests:
-
command_started_event:
command:
aggregate: *collection_name
pipeline:
- $collStats: { count: {} }
- $group: { _id: 1, n: { $sum: $count }}
command_name: aggregate
database_name: *database_name
count: *collection_name
command_name: count
database_name: *database_name