diff --git a/lib/cursor/QueryCursor.js b/lib/cursor/QueryCursor.js index 3a2e683890e..f862d0f4fde 100644 --- a/lib/cursor/QueryCursor.js +++ b/lib/cursor/QueryCursor.js @@ -194,6 +194,24 @@ QueryCursor.prototype.close = function(callback) { }, this.model.events); }; +/** + * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will + * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even + * if the resultant data has already been retrieved by this cursor. + * + * @return {AggregationCursor} this + * @api public + * @method rewind + */ + +QueryCursor.prototype.rewind = function() { + const _this = this; + _waitForCursor(this, function() { + _this.cursor.rewind(); + }); + return this; +}; + /** * Get the next document from this cursor. Will return `null` when there are * no documents left. diff --git a/types/cursor.d.ts b/types/cursor.d.ts index 80e43530dff..bd514542676 100644 --- a/types/cursor.d.ts +++ b/types/cursor.d.ts @@ -26,6 +26,13 @@ declare module 'mongoose' { close(callback: CallbackWithoutResult): void; close(): Promise; + /** + * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will + * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even + * if the resultant data has already been retrieved by this cursor. + */ + rewind(): this; + /** * Execute `fn` for every document(s) in the cursor. If batchSize is provided * `fn` will be executed for each batch of documents. If `fn` returns a promise,