Skip to content

Commit

Permalink
feat(NODE-4185): Allow opting out of disk use on cursor builder (#3230)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxb committed May 5, 2022
1 parent 3269a6e commit d216725
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cursor/find_cursor.ts
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.
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
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();
})
});
});
});

0 comments on commit d216725

Please sign in to comment.