Skip to content

Commit 4b8a25e

Browse files
committed
Fix cloning made pre hook not able to modify options. Reverted changes in AggregationCursor.
1 parent 51e6b08 commit 4b8a25e

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

lib/aggregate.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Aggregate.prototype.options;
9595
*/
9696

9797
Aggregate.prototype._optionsForExec = function() {
98-
const options = clone(this.options || {});
98+
const options = this.options || {};
9999

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

934934
Aggregate.prototype.cursor = function(options) {
935+
this._optionsForExec();
935936
this.options.cursor = options || {};
936937
return new AggregationCursor(this); // return this;
937938
};
@@ -1040,7 +1041,7 @@ Aggregate.prototype.exec = async function exec() {
10401041
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
10411042
applyGlobalDiskUse(this.options, model.db.options, model.base.options);
10421043

1043-
const options = this._optionsForExec()
1044+
this._optionsForExec();
10441045

10451046
if (this.options && this.options.cursor) {
10461047
return new AggregationCursor(this);
@@ -1066,6 +1067,8 @@ Aggregate.prototype.exec = async function exec() {
10661067
throw new MongooseError('Aggregate has empty pipeline');
10671068
}
10681069

1070+
const options = clone(this.options || {});
1071+
10691072
let result;
10701073
try {
10711074
const cursor = await collection.aggregate(this._pipeline, options);

lib/cursor/aggregationCursor.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ function AggregationCursor(agg) {
4444
const model = agg._model;
4545
delete agg.options.cursor.useMongooseAggCursor;
4646
this._mongooseOptions = {};
47-
this.options = {};
4847

4948
_init(model, this, agg);
5049
}
@@ -58,25 +57,21 @@ util.inherits(AggregationCursor, Readable);
5857
function _init(model, c, agg) {
5958
if (!model.collection.buffer) {
6059
model.hooks.execPre('aggregate', agg, function() {
61-
Object.assign(c.options, agg._optionsForExec());
62-
6360
if (typeof agg.options?.cursor?.transform === 'function') {
6461
c._transforms.push(agg.options.cursor.transform);
6562
}
6663

67-
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
64+
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
6865
c.emit('cursor', c.cursor);
6966
});
7067
} else {
7168
model.collection.emitter.once('queue', function() {
7269
model.hooks.execPre('aggregate', agg, function() {
73-
Object.assign(c.options, agg._optionsForExec());
74-
7570
if (typeof agg.options?.cursor?.transform === 'function') {
7671
c._transforms.push(agg.options.cursor.transform);
7772
}
7873

79-
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
74+
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
8075
c.emit('cursor', c.cursor);
8176
});
8277
});

0 commit comments

Comments
 (0)