From 8b7b180f61a3839505fc3d64896eef3171f1c14d Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 24 May 2023 14:24:26 -0400 Subject: [PATCH 1/2] fix: apply midware to query cursors before query --- lib/cursor/QueryCursor.js | 2 +- lib/query.js | 1 + test/query.cursor.test.js | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/cursor/QueryCursor.js b/lib/cursor/QueryCursor.js index 9c04e7a0ecb..bc6cfbcbff1 100644 --- a/lib/cursor/QueryCursor.js +++ b/lib/cursor/QueryCursor.js @@ -46,7 +46,6 @@ function QueryCursor(query, options) { this._transforms = []; this.model = model; this.options = options || {}; - model.hooks.execPre('find', query, (err) => { if (err != null) { _this._markError(err); @@ -66,6 +65,7 @@ function QueryCursor(query, options) { // Max out the number of documents we'll populate in parallel at 5000. this.options._populateBatchSize = Math.min(this.options.batchSize, 5000); } + Object.assign(this.options, query._optionsForExec()); model.collection.find(query._conditions, this.options, (err, cursor) => { if (err != null) { _this._markError(err); diff --git a/lib/query.js b/lib/query.js index e7e7f046cbb..e8f8808dbe0 100644 --- a/lib/query.js +++ b/lib/query.js @@ -5021,6 +5021,7 @@ Query.prototype.cursor = function cursor(opts) { } const options = this._optionsForExec(); + try { this.cast(this.model); } catch (err) { diff --git a/test/query.cursor.test.js b/test/query.cursor.test.js index e38e589726b..b1333f0bb66 100644 --- a/test/query.cursor.test.js +++ b/test/query.cursor.test.js @@ -805,6 +805,28 @@ describe('QueryCursor', function() { const docs = await Example.find().sort('foo'); assert.deepStrictEqual(docs.map(d => d.foo), ['example1', 'example2']); }); + it('should allow middleware to run before applying _optionsForExec() gh-13417', async function() { + const testSchema = new Schema({ + a: Number, + b: Number, + c: Number + }); + testSchema.pre('find', function() { + this.select('-c'); + }); + const Test = db.model('gh13417', testSchema); + await Test.create([{ a: 1, b: 1, c: 1 }, { a: 2, b: 2, c: 2 }]); + const cursorMiddleSelect = []; + let r; + let cursor = Test.find().select('-b').sort({ a: 1 }).cursor(); + while (r = await cursor.next()) { + cursorMiddleSelect.push(r); + } + assert.equal(typeof cursorMiddleSelect[0].b, 'undefined'); + assert.equal(typeof cursorMiddleSelect[1].b, 'undefined'); + assert.equal(typeof cursorMiddleSelect[0].c, 'undefined'); + assert.equal(typeof cursorMiddleSelect[1].c, 'undefined'); + }); }); async function delay(ms) { From 004c7cc4589a2d4f6dbdcca4bab240146a092110 Mon Sep 17 00:00:00 2001 From: Daniel Diaz <39510674+IslandRhythms@users.noreply.github.com> Date: Wed, 24 May 2023 14:25:21 -0400 Subject: [PATCH 2/2] fix: lint --- test/query.cursor.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/query.cursor.test.js b/test/query.cursor.test.js index b1333f0bb66..32052bb3cca 100644 --- a/test/query.cursor.test.js +++ b/test/query.cursor.test.js @@ -818,7 +818,8 @@ describe('QueryCursor', function() { await Test.create([{ a: 1, b: 1, c: 1 }, { a: 2, b: 2, c: 2 }]); const cursorMiddleSelect = []; let r; - let cursor = Test.find().select('-b').sort({ a: 1 }).cursor(); + const cursor = Test.find().select('-b').sort({ a: 1 }).cursor(); + // eslint-disable-next-line no-cond-assign while (r = await cursor.next()) { cursorMiddleSelect.push(r); }