Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-4185): Allow opting out of disk use on cursor builder #3230

Merged
merged 4 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,19 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
* @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);

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.
lerouxb marked this conversation as resolved.
Show resolved Hide resolved
if (!allow) {
this[kBuiltOptions].allowDiskUse = false;
return this;
}

this[kBuiltOptions].allowDiskUse = true;
return this;
}
Expand Down
54 changes: 54 additions & 0 deletions test/integration/crud/find_cursor_methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,58 @@ describe('Find Cursor', function () {
})
});
});

context('#allowDiskUse', function () {
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));

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', {
metadata: { requires: { mongodb: '>=4.4' } },
test: 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();
});
})
});

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();
})
});
});
});