Files
mongo/jstests/aggregation/bugs/server4656.js
Mathias Stearn 061a086641 SERVER-10165 aggregate() shell helper now returns a cursor
The actual change is in src/mongo/shell/collection.js with the remaining changes
to adjust tests to the new api.

The "ideal" arguments are now an array of pipeline ops and an optional object
with extra top-level options for the command (such as {explain: true}. For
backwards compatibility, we still support the varargs style where each argument
is a pipeline stage, but there is no way to specify extra parameters in this
mode.
2013-10-08 17:35:02 -04:00

70 lines
2.0 KiB
JavaScript

// SERVER-4656 optimize $sort followed by $limit
var c = db.c;
c.drop();
NUM_OBJS = 100;
var randoms = {}
function generateRandom() {
// we want unique randoms since $sort isn't guaranteed stable
var random;
do {
random = Math.round(Math.random() * 1000 * NUM_OBJS)
} while (randoms[random]);
randoms[random] = true;
return random;
}
for (var i = 0; i < NUM_OBJS; i++) {
c.insert({inc: i, dec: NUM_OBJS-i, rnd: generateRandom()});
}
var inc_sorted = c.aggregate({$sort: {inc:1}}).toArray();
var dec_sorted = c.aggregate({$sort: {dec:1}}).toArray();
var rnd_sorted = c.aggregate({$sort: {rnd:1}}).toArray();
function test(limit, direction) {
try {
var res_inc = c.aggregate({$sort: {inc:direction}}, {$limit:limit}).toArray();
var res_dec = c.aggregate({$sort: {dec:direction}}, {$limit:limit}).toArray();
var res_rnd = c.aggregate({$sort: {rnd:direction}}, {$limit:limit}).toArray();
var expectedLength = Math.min(limit, NUM_OBJS) ;
assert.eq(res_inc.length, expectedLength);
assert.eq(res_dec.length, expectedLength);
assert.eq(res_rnd.length, expectedLength);
if (direction > 0) {
for (var i = 0; i < expectedLength; i++) {
assert.eq(res_inc[i], inc_sorted[i]);
assert.eq(res_dec[i], dec_sorted[i]);
assert.eq(res_rnd[i], rnd_sorted[i]);
}
}
else {
for (var i = 0; i < expectedLength; i++) {
assert.eq(res_inc[i], inc_sorted[NUM_OBJS - 1 - i]);
assert.eq(res_dec[i], dec_sorted[NUM_OBJS - 1 - i]);
assert.eq(res_rnd[i], rnd_sorted[NUM_OBJS - 1 - i]);
}
}
}
catch (e) {
print("failed with limit=" + limit + " direction= " + direction);
throw e;
}
}
test(1, 1);
test(1, -1);
test(10, 1);
test(10, -1);
test(50, 1);
test(50, -1);
test(NUM_OBJS, 1);
test(NUM_OBJS, -1);
test(NUM_OBJS + 10, 1);
test(NUM_OBJS + 10, -1);