|
@@ -11,19 +11,21 @@
|
|
|
* @method aggregate
|
|
|
* @namespace mungedb
|
|
|
* @module mungedb-aggregate
|
|
|
- * @param pipeline {Array} The list of pipeline document sources in JSON format
|
|
|
- * @param [inputs] {Array} Optional inputs to pass through the `docSrcs` pipeline
|
|
|
- * @param [callback] {Function} Optional callback if using async extensions, called when done
|
|
|
+ * @param pipeline {Array} The list of pipeline document sources in JSON format
|
|
|
+ * @param [ctx] {Object} Optional context object to pass through to pipeline
|
|
|
+ * @param [inputs] {Array} Optional inputs to pass through the `docSrcs` pipeline
|
|
|
+ * @param [callback] {Function} Optional callback if using async extensions, called when done
|
|
|
* @param callback.err {Error} The Error if one occurred
|
|
|
* @param callback.docs {Array} The resulting documents
|
|
|
**/
|
|
|
exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) { // function-style interface; i.e., return the utility function directly as the require
|
|
|
- if (ctx instanceof Array) callback = inputs, inputs = ctx, ctx = {};
|
|
|
+ var DocumentSource = exports.pipeline.documentSources.DocumentSource;
|
|
|
+ if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
|
|
|
var pipelineInst = exports.pipeline.Pipeline.parseCommand({
|
|
|
pipeline: pipeline
|
|
|
}, ctx),
|
|
|
aggregator = function aggregator(ctx, inputs, callback) {
|
|
|
- if (ctx instanceof Array) callback = inputs, inputs = ctx, ctx = {};
|
|
|
+ if (ctx instanceof Array || ctx instanceof DocumentSource) callback = inputs, inputs = ctx, ctx = {};
|
|
|
if (!callback) callback = exports.SYNC_CALLBACK;
|
|
|
if (!inputs) return callback("arg `inputs` is required");
|
|
|
|
|
@@ -36,7 +38,7 @@ exports = module.exports = function aggregate(pipeline, ctx, inputs, callback) {
|
|
|
|
|
|
// use or build input src
|
|
|
var src;
|
|
|
- if(inputs instanceof exports.pipeline.documentSources.DocumentSource){
|
|
|
+ if(inputs instanceof DocumentSource){
|
|
|
src = inputs;
|
|
|
}else{
|
|
|
try{
|