Skip to content

Commit d216725

Browse files
authored
feat(NODE-4185): Allow opting out of disk use on cursor builder (#3230)
1 parent 3269a6e commit d216725

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/cursor/find_cursor.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,19 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
412412
* @remarks
413413
* {@link https://docs.mongodb.com/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}
414414
*/
415-
allowDiskUse(): this {
415+
allowDiskUse(allow = true): this {
416416
assertUninitialized(this);
417+
417418
if (!this[kBuiltOptions].sort) {
418419
throw new MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification');
419420
}
421+
422+
// As of 6.0 the default is true. This allows users to get back to the old behaviour.
423+
if (!allow) {
424+
this[kBuiltOptions].allowDiskUse = false;
425+
return this;
426+
}
427+
420428
this[kBuiltOptions].allowDiskUse = true;
421429
return this;
422430
}

test/integration/crud/find_cursor_methods.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,58 @@ describe('Find Cursor', function () {
294294
})
295295
});
296296
});
297+
298+
context('#allowDiskUse', function () {
299+
it('should set allowDiskUse to true by default', {
300+
metadata: { requires: { mongodb: '>=4.4' } },
301+
test: withClientV2(function (client, done) {
302+
const commands = [];
303+
client.on('commandStarted', filterForCommands(['find'], commands));
304+
305+
const coll = client.db().collection('abstract_cursor');
306+
const cursor = coll.find({}, { sort: 'foo' });
307+
cursor.allowDiskUse();
308+
this.defer(() => cursor.close());
309+
310+
cursor.toArray(err => {
311+
expect(err).to.not.exist;
312+
expect(commands).to.have.length(1);
313+
expect(commands[0].command.allowDiskUse).to.equal(true);
314+
done();
315+
});
316+
})
317+
});
318+
319+
it('should set allowDiskUse to false if specified', {
320+
metadata: { requires: { mongodb: '>=4.4' } },
321+
test: withClientV2(function (client, done) {
322+
const commands = [];
323+
client.on('commandStarted', filterForCommands(['find'], commands));
324+
325+
const coll = client.db().collection('abstract_cursor');
326+
const cursor = coll.find({}, { sort: 'foo' });
327+
cursor.allowDiskUse(false);
328+
this.defer(() => cursor.close());
329+
330+
cursor.toArray(err => {
331+
expect(err).to.not.exist;
332+
expect(commands).to.have.length(1);
333+
expect(commands[0].command.allowDiskUse).to.equal(false);
334+
done();
335+
});
336+
})
337+
});
338+
339+
it('throws if the query does not have sort specified', {
340+
metadata: { requires: { mongodb: '>=4.4' } },
341+
test: withClientV2(function (client, done) {
342+
const coll = client.db().collection('abstract_cursor');
343+
const cursor = coll.find({});
344+
expect(() => cursor.allowDiskUse(false)).to.throw(
345+
'Option "allowDiskUse" requires a sort specification'
346+
);
347+
done();
348+
})
349+
});
350+
});
297351
});

0 commit comments

Comments
 (0)