Skip to content

Commit

Permalink
fix(cursor): cap parallel batchSize for populate at 5000
Browse files Browse the repository at this point in the history
Fix #10449
Re: #9366
  • Loading branch information
vkarpov15 committed Jul 28, 2021
1 parent 4640bee commit 1589af2
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/cursor/QueryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ function QueryCursor(query, options) {
if (this.options.batchSize) {
this.options.cursor = options.cursor || {};
this.options.cursor.batchSize = options.batchSize;

// Max out the number of documents we'll populate in parallel at 5000.
this.options._populateBatchSize = Math.min(this.options.batchSize, 5000);
}
model.collection.find(query._conditions, this.options, function(err, cursor) {
if (_this._error) {
Expand Down Expand Up @@ -360,7 +363,7 @@ function _next(ctx, cb) {
ctx.query._mongooseOptions);
ctx._pop.__noPromise = true;
}
if (ctx.query._mongooseOptions.populate && ctx.options.batchSize > 1) {
if (ctx.query._mongooseOptions.populate && ctx.options._populateBatchSize > 1) {
if (ctx._batchDocs && ctx._batchDocs.length) {
// Return a cached populated doc
return _nextDoc(ctx, ctx._batchDocs.shift(), ctx._pop, callback);
Expand Down Expand Up @@ -418,7 +421,7 @@ function _onNext(error, doc) {

this.ctx._batchDocs.push(doc);

if (this.ctx._batchDocs.length < this.ctx.options.batchSize) {
if (this.ctx._batchDocs.length < this.ctx.options._populateBatchSize) {
this.ctx.cursor.next(_onNext.bind(this));
} else {
_populateBatch.call(this);
Expand Down

0 comments on commit 1589af2

Please sign in to comment.