Skip to content

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

New issue

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

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

Already on GitHub? # to your account

Merged
merged 9 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,15 @@ 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.
*
* due to an oversight in versions 5.0.0-5.0.8 of MongoDB, the count command,
* which estimatedDocumentCount uses in its implementation, was not included in v1 of
* the Stable API, and so users of the Stable API with estimatedDocumentCount are
* recommended to upgrade their server version to 5.0.9+ or set apiStrict: false to avoid
* encountering errors.
*
* @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
31 changes: 2 additions & 29 deletions src/operations/estimated_document_count.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Document } from '../bson';
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 +31,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 @@ -71,7 +44,7 @@ export class EstimatedDocumentCountOperation extends CommandOperation<number> {
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
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
Loading