Skip to content

Commit

Permalink
Fix cloning made pre hook not able to modify options. Reverted change…
Browse files Browse the repository at this point in the history
…s in AggregationCursor.
  • Loading branch information
IchirokuXVI committed Dec 16, 2024
1 parent 51e6b08 commit 4b8a25e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
7 changes: 5 additions & 2 deletions lib/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Aggregate.prototype.options;
*/

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

const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
Expand Down Expand Up @@ -932,6 +932,7 @@ Aggregate.prototype.option = function(value) {
*/

Aggregate.prototype.cursor = function(options) {
this._optionsForExec();
this.options.cursor = options || {};
return new AggregationCursor(this); // return this;
};
Expand Down Expand Up @@ -1040,7 +1041,7 @@ Aggregate.prototype.exec = async function exec() {
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
applyGlobalDiskUse(this.options, model.db.options, model.base.options);

const options = this._optionsForExec()
this._optionsForExec();

if (this.options && this.options.cursor) {
return new AggregationCursor(this);
Expand All @@ -1066,6 +1067,8 @@ Aggregate.prototype.exec = async function exec() {
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: 2 additions & 7 deletions lib/cursor/aggregationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function AggregationCursor(agg) {
const model = agg._model;
delete agg.options.cursor.useMongooseAggCursor;
this._mongooseOptions = {};
this.options = {};

_init(model, this, agg);
}
Expand All @@ -58,25 +57,21 @@ 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());

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

c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
c.cursor = model.collection.aggregate(agg._pipeline, agg.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, c.options || {});
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
c.emit('cursor', c.cursor);
});
});
Expand Down

0 comments on commit 4b8a25e

Please sign in to comment.