Skip to content

Commit a48d7e2

Browse files
authored
feat(NODE-3867): deprecate cursor count and update v4 docs (#3127)
1 parent 323bb8d commit a48d7e2

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

docs/CHANGES_4.0.0.md

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ for await (const doc of cursor) {
9696
Prior to the this release there was inconsistency surrounding how the cursor would error if a setting like limit was applied after cursor execution had begun.
9797
Now, an error along the lines of: `Cursor is already initialized` is thrown.
9898

99+
##### Cursor.count always respects skip and limit
100+
101+
> Updated: Feb 3rd 2022
102+
103+
The `applySkipLimit` argument has been removed from `cursor.count`, cursors will always passthrough the skip and limit to the underlying count operation.
104+
It is recommended that users utilize the `collection.countDocuments` or `collection.estimatedDocumentCount` APIs.
105+
99106
#### ChangeStream must be used as an iterator or an event emitter
100107

101108
You cannot use ChangeStream as an iterator after using as an EventEmitter nor visa versa.

src/cursor/find_cursor.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import type { Hint } from '../operations/operation';
99
import type { Topology } from '../sdam/topology';
1010
import type { ClientSession } from '../sessions';
1111
import { formatSort, Sort, SortDirection } from '../sort';
12-
import type { Callback, MongoDBNamespace } from '../utils';
13-
import { mergeOptions } from '../utils';
12+
import { Callback, emitWarningOnce, mergeOptions, MongoDBNamespace } from '../utils';
1413
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
1514

1615
/** @internal */
@@ -118,15 +117,24 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
118117
});
119118
}
120119

121-
/** Get the count of documents for this cursor */
120+
/**
121+
* Get the count of documents for this cursor
122+
* @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead
123+
*/
122124
count(): Promise<number>;
125+
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead */
123126
count(callback: Callback<number>): void;
127+
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead */
124128
count(options: CountOptions): Promise<number>;
129+
/** @deprecated Use `collection.estimatedDocumentCount` or `collection.countDocuments` instead */
125130
count(options: CountOptions, callback: Callback<number>): void;
126131
count(
127132
options?: CountOptions | Callback<number>,
128133
callback?: Callback<number>
129134
): Promise<number> | void {
135+
emitWarningOnce(
136+
'cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead '
137+
);
130138
if (typeof options === 'boolean') {
131139
throw new MongoInvalidArgumentError('Invalid first parameter to count');
132140
}

test/types/mongodb.test-d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as MongoDBDriver from '../../src';
66
import type { ChangeStreamDocument } from '../../src/change_stream';
77
import { Collection } from '../../src/collection';
88
import { AggregationCursor } from '../../src/cursor/aggregation_cursor';
9-
import type { FindCursor } from '../../src/cursor/find_cursor';
9+
import { FindCursor } from '../../src/cursor/find_cursor';
1010
import { MongoClient } from '../../src/mongo_client';
1111
import { Topology } from '../../src/sdam/topology';
1212

@@ -17,6 +17,7 @@ expectDeprecated(Collection.prototype.remove);
1717
expectDeprecated(Collection.prototype.count);
1818
expectDeprecated(Collection.prototype.mapReduce);
1919
expectDeprecated(AggregationCursor.prototype.geoNear);
20+
expectDeprecated(FindCursor.prototype.count);
2021
expectDeprecated(Topology.prototype.unref);
2122
expectDeprecated(Db.prototype.unref);
2223
expectDeprecated(MongoDBDriver.ObjectID);

0 commit comments

Comments
 (0)