Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix aggregate cursor als #15094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 19 additions & 5 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@

Aggregate.prototype.options;

/**
* Returns default options for this aggregate.
*
* @param {Model} model
* @api private
*/

Aggregate.prototype._optionsForExec = function() {
const options = clone(this.options || {});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clone() shouldn't be necessary here, would be ideal to avoid cloning for performance purposes


const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
options.session = asyncLocalStorage.session;
}

return options;
};

/**
* Get/set the model that this aggregation will execute on.
*
Expand Down Expand Up @@ -1022,10 +1040,7 @@
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
applyGlobalDiskUse(this.options, model.db.options, model.base.options);

const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
this.options.session = asyncLocalStorage.session;
}
const options = this._optionsForExec()

Check failure on line 1043 in lib/aggregate.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

if (this.options && this.options.cursor) {
return new AggregationCursor(this);
Expand All @@ -1051,7 +1066,6 @@
throw new MongooseError('Aggregate has empty pipeline');
}

const options = clone(this.options || {});
let result;
try {
const cursor = await collection.aggregate(this._pipeline, options);
Expand Down
9 changes: 7 additions & 2 deletions lib/cursor/aggregationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function AggregationCursor(agg) {
const model = agg._model;
delete agg.options.cursor.useMongooseAggCursor;
this._mongooseOptions = {};
this.options = {};

_init(model, this, agg);
}
Expand All @@ -57,21 +58,25 @@ util.inherits(AggregationCursor, Readable);
function _init(model, c, agg) {
if (!model.collection.buffer) {
model.hooks.execPre('aggregate', agg, function() {
Object.assign(c.options, agg._optionsForExec());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to previous comments, is there any need to set c.options at all? Could just call aggregate(agg._pipeline, agg.optionsForExec())


if (typeof agg.options?.cursor?.transform === 'function') {
c._transforms.push(agg.options.cursor.transform);
}

c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
c.emit('cursor', c.cursor);
});
} else {
model.collection.emitter.once('queue', function() {
model.hooks.execPre('aggregate', agg, function() {
Object.assign(c.options, agg._optionsForExec());

if (typeof agg.options?.cursor?.transform === 'function') {
c._transforms.push(agg.options.cursor.transform);
}

c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
c.emit('cursor', c.cursor);
});
});
Expand Down
Loading