Skip to content

Modified limitRequest to allow negative limits to pass through to Mongo.... #561

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

Merged
merged 1 commit into from
Mar 23, 2012
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
11 changes: 6 additions & 5 deletions lib/mongodb/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ Cursor.prototype.batchSize = function(batchSize, callback) {
*/
var limitRequest = function(self) {
var requestedLimit = self.limitValue;

if(self.limitValue > 0) {
if (self.batchSizeValue > 0) {
requestedLimit = self.limitValue < self.batchSizeValue ?
self.limitValue : self.batchSizeValue;
var absLimitValue = Math.abs(self.limitValue);
var absBatchValue = Math.abs(self.batchSizeValue);

if(absLimitValue > 0) {
if (absBatchValue > 0) {
requestedLimit = Math.min(absLimitValue, absBatchValue);
}
} else {
requestedLimit = self.batchSizeValue;
Expand Down
60 changes: 60 additions & 0 deletions test/cursor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,66 @@ exports.shouldCorrectlyHandleLimitOnCursor = function(test) {
});
}

/**
* @ignore
* @api private
*/
exports.shouldCorrectlyHandleNegativeOneLimitOnCursor = function(test) {
client.createCollection('test_cursor_negative_one_limit', function(err, collection) {
Step(
function insert() {
var group = this.group();

for(var i = 0; i < 10; i++) {
collection.save({'x':1}, {safe:true}, group());
}
},

function finished() {
collection.find(function(err, cursor) {
cursor.limit(-1, function(err, cursor) {
cursor.toArray(function(err, items) {
test.equal(1, items.length);
// Let's close the db
test.done();
});
});
});
}
);
});
}

/**
* @ignore
* @api private
*/
exports.shouldCorrectlyHandleAnyNegativeLimitOnCursor = function(test) {
client.createCollection('test_cursor_any_negative_limit', function(err, collection) {
Step(
function insert() {
var group = this.group();

for(var i = 0; i < 10; i++) {
collection.save({'x':1}, {safe:true}, group());
}
},

function finished() {
collection.find(function(err, cursor) {
cursor.limit(-5, function(err, cursor) {
cursor.toArray(function(err, items) {
test.equal(5, items.length);
// Let's close the db
test.done();
});
});
});
}
);
});
}

/**
* @ignore
* @api private
Expand Down