Skip to content

Commit 51e6b08

Browse files
authored
Use same options for exec and cursor in Model.aggregate
1 parent ade80c7 commit 51e6b08

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/aggregate.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ function Aggregate(pipeline, model) {
8787

8888
Aggregate.prototype.options;
8989

90+
/**
91+
* Returns default options for this aggregate.
92+
*
93+
* @param {Model} model
94+
* @api private
95+
*/
96+
97+
Aggregate.prototype._optionsForExec = function() {
98+
const options = clone(this.options || {});
99+
100+
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
101+
if (!options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
102+
options.session = asyncLocalStorage.session;
103+
}
104+
105+
return options;
106+
};
107+
90108
/**
91109
* Get/set the model that this aggregation will execute on.
92110
*
@@ -1022,10 +1040,7 @@ Aggregate.prototype.exec = async function exec() {
10221040
applyGlobalMaxTimeMS(this.options, model.db.options, model.base.options);
10231041
applyGlobalDiskUse(this.options, model.db.options, model.base.options);
10241042

1025-
const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
1026-
if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
1027-
this.options.session = asyncLocalStorage.session;
1028-
}
1043+
const options = this._optionsForExec()
10291044

10301045
if (this.options && this.options.cursor) {
10311046
return new AggregationCursor(this);
@@ -1051,7 +1066,6 @@ Aggregate.prototype.exec = async function exec() {
10511066
throw new MongooseError('Aggregate has empty pipeline');
10521067
}
10531068

1054-
const options = clone(this.options || {});
10551069
let result;
10561070
try {
10571071
const cursor = await collection.aggregate(this._pipeline, options);

lib/cursor/aggregationCursor.js

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

4849
_init(model, this, agg);
4950
}
@@ -57,21 +58,25 @@ util.inherits(AggregationCursor, Readable);
5758
function _init(model, c, agg) {
5859
if (!model.collection.buffer) {
5960
model.hooks.execPre('aggregate', agg, function() {
61+
Object.assign(c.options, agg._optionsForExec());
62+
6063
if (typeof agg.options?.cursor?.transform === 'function') {
6164
c._transforms.push(agg.options.cursor.transform);
6265
}
6366

64-
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
67+
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
6568
c.emit('cursor', c.cursor);
6669
});
6770
} else {
6871
model.collection.emitter.once('queue', function() {
6972
model.hooks.execPre('aggregate', agg, function() {
73+
Object.assign(c.options, agg._optionsForExec());
74+
7075
if (typeof agg.options?.cursor?.transform === 'function') {
7176
c._transforms.push(agg.options.cursor.transform);
7277
}
7378

74-
c.cursor = model.collection.aggregate(agg._pipeline, agg.options || {});
79+
c.cursor = model.collection.aggregate(agg._pipeline, c.options || {});
7580
c.emit('cursor', c.cursor);
7681
});
7782
});

0 commit comments

Comments
 (0)