0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00

SERVER-11415 Shell aggregate() helper now supports old servers

This commit is contained in:
Mathias Stearn 2013-11-11 17:47:36 -05:00
parent 613d8b223c
commit 47499ce542
2 changed files with 37 additions and 26 deletions

View File

@ -11,32 +11,26 @@ function testVersions(versions) {
// basic aggregation using just 2.4 features works
var res = coll.runCommand('aggregate', {pipeline: [{$group: {_id: null,
count: {$sum: 1},
avg: {$avg: '$_id'},
}}]});
assert.eq(res.result, [{_id: null, count: 1000, avg: 499.5}]);
var res = coll.aggregate([{$group: {_id: null, count: {$sum: 1}, avg: {$avg: '$_id'}}}]);
assert.eq(res.toArray(), [{_id: null, count: 1000, avg: 499.5}]);
// sorting with 2.4 shards uses a different codepath
var res = coll.runCommand('aggregate', {pipeline: [{$project: {_id:1}},
{$sort: {_id: 1}},
{$limit: 5}]});
assert.eq(res.result, [{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}]);
var res = coll.aggregate([{$project: {_id:1}}, {$sort: {_id: 1}}, {$limit: 5}]);
assert.eq(res.toArray(), [{_id: 0}, {_id: 1}, {_id: 2}, {_id: 3}, {_id: 4}]);
// Targeted aggregation works
var res = coll.runCommand('aggregate', {pipeline: [{$match: {_id: 0}},
{$project: {_id: 1}},
]});
assert.eq(res.result, [{_id: 0}]);
var res = coll.aggregate([{$match: {_id: 0}}, {$project: {_id: 1}}]);
assert.eq(res.toArray(), [{_id: 0}]);
if (testFailures) {
// 2.6 features aren't guaranteed to work until upgrade is complete. They may work
// anyway if all data is on upgraded shards and the primary is updated, which is why
// these only run in the "normal" sharded case
assert.throws(function() {coll.aggregate({$limit: 100000})});
assert.commandFailed(coll.runCommand('aggregate', {pipeline: [{$out: "ts1_out"}]}));
assert.commandFailed(coll.runCommand('aggregate', {pipeline: [{$limit: 10}],
allowDiskUsage:true}));
allowDiskUsage:true}));
assert.commandFailed(coll.runCommand('aggregate', {pipeline: [{$limit: 10}],
cursor: {}}));
}
}

View File

@ -985,25 +985,42 @@ DBCollection.prototype.distinct = function( keyString , query ){
DBCollection.prototype.aggregate = function(pipeline, extraOpts) {
var cmd = {pipeline: pipeline};
if (!(pipeline instanceof Array)) {
// support varargs form
cmd.pipeline = [];
for (var i=0; i<arguments.length; i++) {
cmd.pipeline.push(arguments[i]);
}
// support legacy varargs form. (Also handles db.foo.aggregate())
pipeline = argumentsToArray(arguments)
extraOpts = {}
}
else {
Object.extend(cmd, extraOpts);
else if (extraOpts === undefined) {
extraOpts = {};
}
if (cmd.cursor === undefined) {
var cmd = {pipeline: pipeline};
Object.extend(cmd, extraOpts);
if (!('cursor' in cmd)) {
// implicitly use cursors
cmd.cursor = {};
}
var res = this.runCommand("aggregate", cmd);
assert.commandWorked(res, "aggregate with cursor failed");
if (!res.ok
&& (res.code == 17020 || res.errmsg == "unrecognized field \"cursor")
&& !("cursor" in extraOpts)) {
// If the command failed because cursors aren't supported and the user didn't explicitly
// request a cursor, try again without requesting a cursor.
delete cmd.cursor;
res = this.runCommand("aggregate", cmd);
if ('result' in res && !("cursor" in res)) {
// convert old-style output to cursor-style output
res.cursor = {ns: '', id: NumberLong(0)};
res.cursor.firstBatch = res.result;
delete res.result;
}
}
assert.commandWorked(res, "aggregate failed");
if ("cursor" in res)
return new DBCommandCursor(this._mongo, res);