Browse Source

Merge pull request #204 from papajuans/master

Allow array-based aggregation pipelines
saintedlama/travis-non-legacy
Christoph Walcher 9 years ago
parent
commit
1cf7df09d0
  1. 4
      lib/collection.js
  2. 41
      test/test-aggregate-pipeline.js

4
lib/collection.js

@ -255,6 +255,10 @@ Collection.prototype.aggregate = function() {
cb = once(pipeline.pop());
}
if (pipeline.length === 1 && Array.isArray(pipeline[0])) {
pipeline = pipeline[0];
}
if (cb) {
this.runCommand('aggregate', {pipeline: pipeline}, function(err, res) {
if (err) return cb(err);

41
test/test-aggregate-pipeline.js

@ -0,0 +1,41 @@
var insert = require('./insert');
var concat = require('concat-stream');
insert('aggregate', [{
name:'Squirtle', type:'water'
}, {
name:'Starmie' , type:'water'
}, {
name:'Charmander' , type:'fire'
}, {
name:'Lapras' , type:'water'
}], function(db, t, done) {
db.a.aggregate([{$group: {_id: '$type'}}, {$project: { _id: 0, foo: "$_id" }}], function(err, types) {
console.log(err, types);
var arr = types.map(function(x) {return x.foo});
console.log('arr', arr);
t.equal(types.length, 2);
console.log('here');
t.notEqual(arr.indexOf('fire'), -1);
console.log('there');
t.notEqual(arr.indexOf('water'), -1);
console.log('where');
// test as a stream
var strm = db.a.aggregate([{$group: {_id: '$type'}}, {$project: {_id: 0, foo: "$_id"}}])
strm.pipe(concat(function(types) {
var arr = types.map(function(x) {return x.foo});
t.equal(types.length, 2);
t.notEqual(arr.indexOf('fire'), -1);
t.notEqual(arr.indexOf('water'), -1);
t.end();
}));
strm.on('error', function(err) {
// Aggregation cursors are only supported on mongodb 2.6+
// this shouldn't fail the tests for other versions of mongodb
if (err.message === 'unrecognized field "cursor') t.ok(1);
else t.fail(err);
t.end();
});
});
});
Loading…
Cancel
Save