diff --git a/lib/cursor/QueryCursor.js b/lib/cursor/QueryCursor.js index 9c016a2facb..f38229bd848 100644 --- a/lib/cursor/QueryCursor.js +++ b/lib/cursor/QueryCursor.js @@ -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) { @@ -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); @@ -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);