From c573bd17a2c6a6c11f55947c57a685d2e5d83f6d Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Tue, 3 May 2022 14:14:05 +0100 Subject: [PATCH 1/4] Allow opting out of disk use --- src/cursor/find_cursor.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cursor/find_cursor.ts b/src/cursor/find_cursor.ts index 2bf9cffd2d..130a64e014 100644 --- a/src/cursor/find_cursor.ts +++ b/src/cursor/find_cursor.ts @@ -412,8 +412,15 @@ export class FindCursor extends AbstractCursor { * @remarks * {@link https://docs.mongodb.com/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation} */ - allowDiskUse(): this { + allowDiskUse(allow = true): this { assertUninitialized(this); + + // As of 6.0 the default is true. This allows users to get back to the old behaviour. + if (!allow) { + this[kBuiltOptions].allowDiskUse = false; + return this; + } + if (!this[kBuiltOptions].sort) { throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification'); } From ceec576fba68a4093ed76e8def4f3d7b487f62cb Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 4 May 2022 10:58:48 +0100 Subject: [PATCH 2/4] tests for allowDiskUse --- .../crud/find_cursor_methods.test.js | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/integration/crud/find_cursor_methods.test.js b/test/integration/crud/find_cursor_methods.test.js index abaaf5bbaa..4cce162b3f 100644 --- a/test/integration/crud/find_cursor_methods.test.js +++ b/test/integration/crud/find_cursor_methods.test.js @@ -294,4 +294,46 @@ describe('Find Cursor', function () { }) }); }); + + context('#allowDiskUse', function () { + it( + 'should set allowDiskUse to true by default', + withClientV2(function (client, done) { + const commands = []; + client.on('commandStarted', filterForCommands(['find'], commands)); + + const coll = client.db().collection('abstract_cursor'); + const cursor = coll.find({}, { sort: 'foo' }); + cursor.allowDiskUse(); + this.defer(() => cursor.close()); + + cursor.toArray(err => { + expect(err).to.not.exist; + expect(commands).to.have.length(1); + expect(commands[0].command.allowDiskUse).to.equal(true); + done(); + }); + }) + ); + + it( + 'should set allowDiskUse to false if specified', + withClientV2(function (client, done) { + const commands = []; + client.on('commandStarted', filterForCommands(['find'], commands)); + + const coll = client.db().collection('abstract_cursor'); + const cursor = coll.find({}, { sort: 'foo' }); + cursor.allowDiskUse(false); + this.defer(() => cursor.close()); + + cursor.toArray(err => { + expect(err).to.not.exist; + expect(commands).to.have.length(1); + expect(commands[0].command.allowDiskUse).to.equal(false); + done(); + }); + }) + ); + }); }); From 641fb925202392e00129f4088644a1abe888f7e1 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 4 May 2022 11:38:38 +0100 Subject: [PATCH 3/4] allowDiskUse was only added in 4.4 --- .../integration/crud/find_cursor_methods.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/integration/crud/find_cursor_methods.test.js b/test/integration/crud/find_cursor_methods.test.js index 4cce162b3f..2fe7d3dc3a 100644 --- a/test/integration/crud/find_cursor_methods.test.js +++ b/test/integration/crud/find_cursor_methods.test.js @@ -296,9 +296,9 @@ describe('Find Cursor', function () { }); context('#allowDiskUse', function () { - it( - 'should set allowDiskUse to true by default', - withClientV2(function (client, done) { + it('should set allowDiskUse to true by default', { + metadata: { requires: { mongodb: '>=4.4' } }, + test: withClientV2(function (client, done) { const commands = []; client.on('commandStarted', filterForCommands(['find'], commands)); @@ -314,11 +314,11 @@ describe('Find Cursor', function () { done(); }); }) - ); + }); - it( - 'should set allowDiskUse to false if specified', - withClientV2(function (client, done) { + it('should set allowDiskUse to false if specified', { + metadata: { requires: { mongodb: '>=4.4' } }, + test: withClientV2(function (client, done) { const commands = []; client.on('commandStarted', filterForCommands(['find'], commands)); @@ -334,6 +334,6 @@ describe('Find Cursor', function () { done(); }); }) - ); + }); }); }); From 513bcb1fa07d578c44878ef33536d66e9ba3c4d0 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Thu, 5 May 2022 10:30:49 +0100 Subject: [PATCH 4/4] throw if sort is not specified --- src/cursor/find_cursor.ts | 7 ++++--- test/integration/crud/find_cursor_methods.test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cursor/find_cursor.ts b/src/cursor/find_cursor.ts index 130a64e014..ae483ca215 100644 --- a/src/cursor/find_cursor.ts +++ b/src/cursor/find_cursor.ts @@ -415,15 +415,16 @@ export class FindCursor extends AbstractCursor { allowDiskUse(allow = true): this { assertUninitialized(this); + if (!this[kBuiltOptions].sort) { + throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification'); + } + // As of 6.0 the default is true. This allows users to get back to the old behaviour. if (!allow) { this[kBuiltOptions].allowDiskUse = false; return this; } - if (!this[kBuiltOptions].sort) { - throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification'); - } this[kBuiltOptions].allowDiskUse = true; return this; } diff --git a/test/integration/crud/find_cursor_methods.test.js b/test/integration/crud/find_cursor_methods.test.js index 2fe7d3dc3a..992e40d474 100644 --- a/test/integration/crud/find_cursor_methods.test.js +++ b/test/integration/crud/find_cursor_methods.test.js @@ -335,5 +335,17 @@ describe('Find Cursor', function () { }); }) }); + + it('throws if the query does not have sort specified', { + metadata: { requires: { mongodb: '>=4.4' } }, + test: withClientV2(function (client, done) { + const coll = client.db().collection('abstract_cursor'); + const cursor = coll.find({}); + expect(() => cursor.allowDiskUse(false)).to.throw( + 'Option "allowDiskUse" requires a sort specification' + ); + done(); + }) + }); }); });