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(FindCursor): fluent builder for allowDiskUse option #2678

Merged
merged 6 commits into from
Dec 21, 2020
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
15 changes: 15 additions & 0 deletions src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ export class FindCursor extends AbstractCursor {
return this;
}

/**
* Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher)
*
* @remarks
* {@link https://docs.mongodb.com/manual/reference/command/find/#find-cmd-allowdiskuse | find command allowDiskUse documentation}
*/
allowDiskUse(): this {
mbroadst marked this conversation as resolved.
Show resolved Hide resolved
assertUninitialized(this);
if (!this[kBuiltOptions].sort) {
throw new MongoError('allowDiskUse requires a sort specification');
}
this[kBuiltOptions].allowDiskUse = true;
return this;
}

/**
* Set the collation options for the cursor.
*
Expand Down
2 changes: 1 addition & 1 deletion src/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface FindOptions extends CommandOperationOptions {
noCursorTimeout?: boolean;
/** Specify collation (MongoDB 3.4 or higher) settings for update operation (see 3.4 documentation for available fields). */
collation?: CollationOptions;
/** Enables writing to temporary files on the server. */
/** Allows disk use for blocking sort operations exceeding 100MB memory. (MongoDB 3.2 or higher) */
allowDiskUse?: boolean;
/** Determines whether to close the cursor after the first batch. Defaults to false. */
singleBatch?: boolean;
Expand Down
34 changes: 33 additions & 1 deletion test/functional/cursor.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const { assert: test, filterForCommands, withMonitoredClient } = require('./shared');
const { assert: test, filterForCommands, withClient, withMonitoredClient } = require('./shared');
const { setupDatabase } = require('./shared');
const fs = require('fs');
const { expect } = require('chai');
Expand Down Expand Up @@ -4089,5 +4089,37 @@ describe('Cursor', function () {
beta: 1
});
});

it('should use allowDiskUse option on sort', {
metadata: { requires: { mongodb: '>=4.4' } },
test: withMonitoredClient('find', function (client, events, done) {
const db = client.db('test');
db.collection('test_sort_allow_disk_use', (err, collection) => {
expect(err).to.not.exist;
const cursor = collection.find({}).sort(['alpha', 1]).allowDiskUse();
cursor.next(err => {
expect(err).to.not.exist;
const { command } = events.shift();
expect(command.sort).to.deep.equal({ alpha: 1 });
expect(command.allowDiskUse).to.be.true;
cursor.close(done);
});
});
})
});

it('should error if allowDiskUse option used without sort', {
metadata: { requires: { mongodb: '>=4.4' } },
test: withClient(function (client, done) {
const db = client.db('test');
db.collection('test_sort_allow_disk_use', (err, collection) => {
expect(err).to.not.exist;
expect(() => collection.find({}).allowDiskUse()).to.throw(
/allowDiskUse requires a sort specification/
);
done();
});
})
});
});
});