From 47499ce5427e190d76eea978f8e212d9b71f05b0 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Mon, 11 Nov 2013 17:47:36 -0500 Subject: [PATCH] SERVER-11415 Shell aggregate() helper now supports old servers --- .../aggregation_sharding_24_to_26.js | 24 +++++------- src/mongo/shell/collection.js | 39 +++++++++++++------ 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/jstests/multiVersion/aggregation_sharding_24_to_26.js b/jstests/multiVersion/aggregation_sharding_24_to_26.js index fcdddb97edc..006fb4eb730 100644 --- a/jstests/multiVersion/aggregation_sharding_24_to_26.js +++ b/jstests/multiVersion/aggregation_sharding_24_to_26.js @@ -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: {}})); } } diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js index 2f24d3d2ee9..effebca7991 100644 --- a/src/mongo/shell/collection.js +++ b/src/mongo/shell/collection.js @@ -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